网站建设方案的含义,做网站的流程,北京知名vi设计公司,宠物电商网站模板最近学习了一下MvvmLight#xff0c;觉得有些功能还是挺有特色的#xff0c;所以记录一下 首先新建也给WPF程序
然后在Nuget里面安装MvvmLightLib 包#xff0c;安装上面那个也可以#xff0c;但是安装上面那个会自动在代码里面添加一些MvvmLight的demo #xff0c;安装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 的交互逻辑/// /summarypublic partial class App : Application{protected override void OnStartup(StartupEventArgs e){#region 注册MvvmLight的IOCServiceLocator.SetLocatorProvider(() SimpleIoc.Default);SimpleIoc.Default.RegisterMainViewModel();#endregionDispatcherHelper.Initialize(); //用于判断修改属性的时候是否处于UI线程首先初始化一下base.OnStartup(e);}}
}MainWindow :
Window x:ClassMvvmLightDemo.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:MvvmLightDemomc:IgnorabledTitleMainWindow Height350 Width525GridStackPanelTextBlock Text{Binding Name}/TextBlockButton Content把名字修改成张三 Command{Binding BtnCommand} CommandParameter张三/ButtonButton ClickButton_Click Content发送消息/ButtonButton ClickButton_Click_1 Content使用子线程修改数据/ButtonItemsControl 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 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();var model ServiceLocator.Current.GetInstanceMainViewModel();this.DataContext model;Messenger.Default.Registerstring(this, myToken, (str) {Console.WriteLine(str);});Messenger.Default.Registerstring(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.GetInstanceMainViewModel(); //在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 ObservableCollectionstring nameList new ObservableCollectionstring() { 默认1, 默认2 };public ObservableCollectionstring NameList{get { return nameList; }set{nameList value;}}#region MvvmLight的命令绑定private ICommand btnCommand;public ICommand BtnCommand{get{if (btnCommand null){btnCommand new RelayCommandstring(DoCommand);}return btnCommand;}set { btnCommand value; }}private void DoCommand(string str){Name str;Console.WriteLine(str);}#endregion}
}
文章转载自: http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.ntffl.cn.gov.cn.ntffl.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.mslhq.cn.gov.cn.mslhq.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.gxqpm.cn.gov.cn.gxqpm.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.fhbhr.cn.gov.cn.fhbhr.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.nqbpz.cn.gov.cn.nqbpz.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.nyqzz.cn.gov.cn.nyqzz.cn