WPF View与ViewModel的交互

WPF View与ViewModel的交互

1.WPF中的View 即视图,用来显示的界面,可能是一个窗体,一个对话框等。
2.WPF中的ViewModel 即与View对应的视图模型,用来完成底部的数据操作与更新View显示。
3.初步实现逻辑
a.继承INotifyPropertyChanged接口,实现组件属性更改时引发的PropertyChangedEventHandler事件

    class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

b.在ViewModel中继承NotificationObject类,定义需要的字段与属性,当属性值更改时实现值的传递

        private double  input1;
        public double Input1
        {
            get { return input1; }
            set
            {
                input1 = value;
                this.RaisePropertyChanged("Input1");
            }
        }

c.定义DelegateCommand类继承ICommand接口,实现接口的方法

    class DelegateCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteFunc == null)
                return true;
           return this.CanExecuteFunc(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (this.ExecuteAction == null)
                return;
            this.ExecuteAction(parameter);
        }

        public Action<object> ExecuteAction { get; set; }

        public Func<object,bool> CanExecuteFunc { get; set; }
    }

d.在ViewModel中实现需要的逻辑方法

        public DelegateCommand AddCommand { get; set; }
        private void Add(object parameter)
        {
            this.Result = this.Input1 + this.Input2;
        }

        public MainWindowViewModel()
        {
            this.AddCommand = new DelegateCommand();
            this.AddCommand.ExecuteAction = new Action<object>(this.Add);
        }

4.最后,在View中添加控件的绑定,实现交互

 this.DataContext = new MainWindowViewModel();
 
<Button x:Name="addButton" Command="{Binding AddCommand}"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一个用于创建 Windows 应用程序的框架,而 Prism 是一个基于 WPF 的框架,它提供了一组设计模式和工具,用于帮助开发人员创建可扩展、可重用和易于维护的应用程序。 在 Prism 中,ViewModel 是一个重要的组件,它的作用是将 View(即用户界面)与 Model(即数据和业务逻辑)解耦,使得两者可以独立进行开发和测试。 在 Prism 中,ViewModel 通常是一个简单的类,它实现了 INotifyPropertyChanged 接口,用于实现数据绑定。ViewModel 还包含了一些命令(Command),用于处理用户交互事件,比如按钮点击、菜单选择等。ViewModel 还可以使用服务(Service)来访问 Model,以获取或操作数据。 下面是一个简单的 ViewModel 的示例: ```csharp public class MainViewModel : BindableBase { private readonly IMyService _myService; private string _name; public string Name { get { return _name; } set { SetProperty(ref _name, value); } } private ICommand _helloCommand; public ICommand HelloCommand { get { if (_helloCommand == null) { _helloCommand = new DelegateCommand(ExecuteHelloCommand); } return _helloCommand; } } public MainViewModel(IMyService myService) { _myService = myService; } private void ExecuteHelloCommand() { string message = _myService.GetMessage(Name); MessageBox.Show(message); } } ``` 在上面的代码中,MainViewModel 包含了一个字符串属性 Name 和一个 ICommand 属性 HelloCommand。Name 属性用于将用户界面中的文本框绑定到 ViewModel 中,而 HelloCommand 则表示用户点击“Hello”按钮时要执行的命令。HelloCommand 的实现是通过调用一个服务(IMyService)来获取一条消息,然后通过 MessageBox 显示出来。 需要注意的是,MainViewModel 的构造函数中注入了一个 IMyService 接口,这个接口封装了对数据和业务逻辑的访问,使得 ViewModel 可以与具体的实现解耦。这种依赖注入的方式可以使得应用程序的组件更加松散耦合,更加易于维护和测试。 总之,ViewModelWPF Prism 中非常重要的一个组件,它用于实现视图与模型之间的解耦和交互。开发人员可以使用 ViewModel 实现数据绑定、命令处理、服务访问等功能,从而创建可扩展、可重用和易于维护的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值