网站建设方案的含义,asp网站下载,网站开发 0755,邢台招聘信息最新招聘2023最近学习了一下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}
}