MVVM框架-MVVMLight

项目URL:http://www.mvvmlight.net/

一、安装MVVMLight

在NuGet程序包中搜索MVVMLight,然后安装。

 

二、使用

 安装完MVVMLight后项目中会自动生成ViewModel目录,并且目录中会生成ViewModelLocator.cs文件

MVVM框架-MVVMLight_第1张图片

App.xaml会自动添加代码,注册全局变量Locator

    
        
            
        
    

建立Model目录放置model类,View目录放置view类

MVVM框架-MVVMLight_第2张图片

 在Model下添加PeopleModel.cs,继承ObservableObject类,ObservableObject实现了INotifyPropertyChanged接口,所有PeopleModel的属性改变可以通知控件绑定属性

    public class PeopleModel : ObservableObject
    {
        private string name = "";
        public string Name { get => name; set => Set(ref name, value); }
    }

 在ViewModel下添加PeopleViewModel.cs,继承ViewModelBase类,ViewModelBase类继承ObservableObject类

    public class PeopleViewModel : ViewModelBase
    {
        private PeopleModel people = new PeopleModel();
        public PeopleModel People { get => people; set => Set(ref people, value); }
//不带参数命令
        public ICommand CmdUpdateName
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    People.Name = "无参数";
                }));
            }
        }
//带参数命令
        public ICommand CmdUpdateName1
        {
            get
            {
                return new RelayCommand(new Action(t =>
                {
                    People.Name = System.Convert.ToString(t);
                }));
            }
        }
    }  
    
   

在ViewModelLocator.cs里注册PeopleViewModel元素

    /// 
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// 
    public class ViewModelLocator
    {
        /// 
        /// Initializes a new instance of the ViewModelLocator class.
        /// 
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view services and models
            ////    SimpleIoc.Default.Register();
            ////}
            ////else
            ////{
            ////    // Create run time view services and models
            ////    SimpleIoc.Default.Register();
            ////}

            SimpleIoc.Default.Register();
            SimpleIoc.Default.Register();
        }

        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance();
            }
        }

        public PeopleViewModel PeopleVM
        {
            get
            {
                return ServiceLocator.Current.GetInstance();
            }
        }

        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }  

在View目录下添加PeopleWindow窗体,并给window的DataContext属性绑定PeopleVM元素

DataContext="{Binding Source={StaticResource Locator}, Path=PeopleVM}">
    
        

 界面效果:

(1)点击无参数按钮:

MVVM框架-MVVMLight_第3张图片

(2)点击有参数按钮

MVVM框架-MVVMLight_第4张图片

 

 

参考:

https://www.cnblogs.com/manupstairs/p/4890300.html 里面有对MVVMLight比较详细的解释

转载于:https://www.cnblogs.com/yaosj/p/11246105.html

你可能感兴趣的:(MVVM框架-MVVMLight)