当前位置: 首页 > news >正文

长沙仿站模板网站建设网络推广渠道分类

长沙仿站模板网站建设,网络推广渠道分类,9e做网站,wordpress模板赚钱最近学习了一下MvvmLight,觉得有些功能还是挺有特色的,所以记录一下 首先新建也给WPF程序 然后在Nuget里面安装MvvmLightLib 包,安装上面那个也可以,但是安装上面那个会自动在代码里面添加一些MvvmLight的demo ,安装M…

最近学习了一下MvvmLight,觉得有些功能还是挺有特色的,所以记录一下

首先新建也给WPF程序

然后在Nuget里面安装MvvmLightLib 包,安装上面那个也可以,但是安装上面那个会自动在代码里面添加一些MvvmLight的demo ,安装MvvmLightLib比较纯净

安装完成后,在App.cs 里面重写一下OnStartup方法,让程序启动的时候初始一下IOC容器和DispatcherHelper。(其实这两步也可以放在其他地方,比如放在构造函数里面或者其他地方也是可以的,没有特殊要求)

 MvvmLight的依赖属性

新建一个 MainViewModel 类 

让实体类继承ViewModelBase类,然后在属性的set访问器里面加上RaisePropertyChanged();就实现了MVVM,这比WPF原生的MVVM简单很多

MvvmLight的命令绑定

然后在界面上绑定命令:

 <Button Content="把名字修改成张三" Command="{Binding BtnCommand}" CommandParameter="张三"></Button>

这个是带参数的命令,如果不需要带参数,那么直接把参数删掉就行

MvvmLight的messenger

这个是MvvmLight的最有亮点的功能了

Messenger.Default.Send("aaaa", "myToken");

第一个参数是发送消息的内容,第二个参数是token的名称,所有此toten的注册者都能收到消息

按钮点击发送后,构造函数中的两个注册者都能收到消息。注册者可以在Model中,或者在其他的地方都能收到,这比使用委托更简单。

MvvmLight跨线程访问控件

DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                });

 这个方法可以跨线程访问控件,在实体类中不能用invoke的时候,就可以用这种方法

以上就是MvvmLight最实用的功能,其他花哨的功能我感觉用处不大

以下是完整代码:

app.cs

using CommonServiceLocator;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Threading;
using MvvmLightDemo.ViewModels;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;namespace MvvmLightDemo
{/// <summary>/// App.xaml 的交互逻辑/// </summary>public partial class App : Application{protected override void OnStartup(StartupEventArgs e){#region 注册MvvmLight的IOCServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);SimpleIoc.Default.Register<MainViewModel>();#endregionDispatcherHelper.Initialize();      //用于判断修改属性的时候是否处于UI线程,首先初始化一下base.OnStartup(e);}}
}

 MainWindow

<Window x:Class="MvvmLightDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:MvvmLightDemo"mc:Ignorable="d"Title="MainWindow" Height="350" Width="525"><Grid><StackPanel><TextBlock Text="{Binding Name}"></TextBlock><Button Content="把名字修改成张三" Command="{Binding BtnCommand}" CommandParameter="张三"></Button><Button Click="Button_Click" Content="发送消息"></Button><Button Click="Button_Click_1"  Content="使用子线程修改数据"></Button><ItemsControl ItemsSource="{Binding NameList}"></ItemsControl></StackPanel></Grid>
</Window>
using CommonServiceLocator;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using MvvmLightDemo.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace MvvmLightDemo
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();var model = ServiceLocator.Current.GetInstance<MainViewModel>();this.DataContext = model;Messenger.Default.Register<string>(this, "myToken", (str) =>{Console.WriteLine(str);});Messenger.Default.Register<string>(this, "myToken", (str) =>{Console.WriteLine(str);});}private void Button_Click(object sender, RoutedEventArgs e){Messenger.Default.Send("aaaa", "myToken");}private void Button_Click_1(object sender, RoutedEventArgs e){Task.Run(() => {DispatcherHelper.CheckBeginInvokeOnUI(() =>{var model = ServiceLocator.Current.GetInstance<MainViewModel>();        //在IOC容器中获取单例model.NameList.Add("aaa");model.NameList.Add("bbb");model.Name = "34242";});});}}
}

MainViewModel

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace MvvmLightDemo.ViewModels
{public class MainViewModel : ViewModelBase{#region MvvmLight的依赖属性private string name = "默认名字";public string Name{get { return name; }set{name = value;RaisePropertyChanged();     //加上了这行代码,属性就具有了通知功能}}#endregionprivate ObservableCollection<string> nameList = new ObservableCollection<string>() { "默认1", "默认2" };public ObservableCollection<string> NameList{get { return nameList; }set{nameList = value;}}#region MvvmLight的命令绑定private ICommand btnCommand;public ICommand BtnCommand{get{if (btnCommand == null){btnCommand = new RelayCommand<string>(DoCommand);}return btnCommand;}set { btnCommand = value; }}private void DoCommand(string str){Name = str;Console.WriteLine(str);}#endregion}
}

http://www.hengruixuexiao.com/news/33980.html

相关文章:

  • 做移动网站优化排名十大互联网平台
  • 网站如何做关键词seo腾讯广告推广平台
  • 网站初期做几个比较好娄底地seo
  • 巴中网站建设seo sem关键词优化
  • 博客网站开发搜索引擎seo是什么
  • 网站 手机版 电脑版 怎么做下载百度2023最新版安装
  • wordpress网站下载文件长尾关键词挖掘精灵官网
  • 辽宁网站建设百度的营销中心上班怎么样
  • 用rp怎么做网站按钮下拉框简述搜索引擎的工作原理
  • 南宁seo建站域名停靠网页推广大全
  • 怎么注册域名免费谷歌seo软件
  • 南宁网站推广公司如何在百度推广
  • 微信小程序开发报价优化seo方案
  • 宝塔做网站安全吗技术培训班
  • 网站每天一条推送怎么做的江门关键词优化公司
  • 正规的饰品行业网站开发百度前三推广
  • 汽车网站建设方案刷赞网站推广空间免费
  • 100元建网站网络培训研修总结
  • 做网站编程在程序如何在百度推广自己的产品
  • 网站和自媒体都可以做常熟网络推广
  • 网站建设功能规划网站交换链接友情链接的作用
  • 对招聘网站页面设计做建议一个新品牌如何推广
  • 淘宝上做网站的可靠百度推广河南总部
  • 企业官网网站设计nba最新交易一览表
  • 网页设计 做网站的代码女生学电子商务好吗
  • 17网站一起做网店普宁轻纺城温馨百度网页入口官网
  • wordpress登陆后查看网站的seo优化报告
  • 使用magento的网站优化搜狗排名
  • html网站开发工具泉州百度竞价公司
  • 智能建站推荐百度登录首页