我是靠谱客的博主 神勇店员,这篇文章主要介绍WPF MvvmLight的使用,现在分享给大家,希望可以做个参考。

WPF MvvmLight的使用

一.在新建的项目中管理NuGet程序包,如下图,下载并安装:
在这里插入图片描述
我们会看到安装成功后项目中多了一个文件夹ViewModel,内部有两个文件:MainViewModel.cs 和 ViewModelLocator.cs(有时候生成的这个文件中的命名空间有错误,需要重新添加,此文件不用也可删除)

二.在MainViewModel 类中实现关联命令和属性;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using GalaSoft.MvvmLight; namespace WPF0316e.ViewModel { public class MainViewModel : ViewModelBase { public MainViewModel() { RunCommand = new GalaSoft.MvvmLight.Command.RelayCommand(Run); Name = "Hello"; } private string name; public string Name { get { return name; } set { name = value; RaisePropertyChanged(); } } public GalaSoft.MvvmLight.Command.RelayCommand RunCommand { get; set; } public void Run() { } } }

三.主窗体Xaml代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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; using WPF0316e.ViewModel; namespace WPF0316e { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainViewModel(); } } }

最后

以上就是神勇店员最近收集整理的关于WPF MvvmLight的使用的全部内容,更多相关WPF内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部