阅读 125

WPF MVVM Prism

  1. View负责前端展示,与ViewModel进行数据和命令的交互。
  2. ViewModel,负责前端视图业务级别的逻辑结构组织,并将其反馈给前端。
  3. Model,主要负责数据实体的结构处理,与ViewModel进行交互。

命令创建方式

方式一:

public DelegateCommand GetTextCommnd { get; set; }
public MainWindowViewModel()
{
    this.GetTextCommnd = new DelegateCommand(new Action(ExecuteGetTextCommnd));
}
void ExecuteGetTextCommnd()
{
    System.Windows.MessageBox.Show(Obj.Text);
}

方式二:

public DelegateCommand

方式三:

private DelegateCommand _getTextCommnd;
public DelegateCommand GetTextCommnd => _getTextCommnd ?? (_getTextCommnd = new DelegateCommand(ExecuteGetTextCommnd));

void ExecuteGetTextCommnd()
{
    System.Windows.MessageBox.Show(Obj.Text);
}

  


 

项目结构:

 

 

Model 类

using Prism.Mvvm;

namespace WpfPrism.Models
{
    public class MainWindowModel : BindableBase
    {
        private string _text;
        public string Text
        {
            get { return _text; }
            set { SetProperty(ref _text, value); }
        }
    }
}

  

ViewModel 类

using System;
using Prism.Commands;
using Prism.Mvvm;
using WpfPrism.Models;

namespace WpfPrism.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel()
        {
            Obj = new MainWindowModel();
            Obj.Text = "默认值";
        }
        public MainWindowModel Obj { set; get; }

        private DelegateCommand _setTextCommnd;
        public DelegateCommand SetTextCommnd =>
            _setTextCommnd ?? (_setTextCommnd = new DelegateCommand(ExecuteSetTextCommnd));

        void ExecuteSetTextCommnd()
        {
            Obj.Text = "已赋新值";
        }

        private DelegateCommand _getTextCommnd;
        public DelegateCommand GetTextCommnd =>
            _getTextCommnd ?? (_getTextCommnd = new DelegateCommand(ExecuteGetTextCommnd));

        void ExecuteGetTextCommnd()
        {
            System.Windows.MessageBox.Show(Obj.Text);
        }
    }
}

前端 XAML


    
        
        

后台 CS

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel();
}

  

 

原文:https://www.cnblogs.com/microsoft-zh/p/14945850.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐