背景:

  需求:实现从数据库读取级联表指定字段数据,并展示到前台界面。

  VM层做业务逻辑层,每页最多获取2条数据。

  View层只有数据表格,上一页与下一页按钮,且上一页与下一页在特定条件下不可用。

 (转载请注明来源:cnblogs coder-fang)

                      解决方案结构如下:

      • 项目结构:       C# 实践之 基于WPF的mvvm模型,使UI独立,逻辑可测
      • WPFTest:主要是界面显示数据(V层),ViewModel是wpftest的VM层,unittest做vm及数据的测试项目。
      • 示例用的数据结构如图:C# 实践之 基于WPF的mvvm模型,使UI独立,逻辑可测

       

 

  1. 创建类库VM项目,为了简化,这里将M层与VM层放到了同一项目中,首先在VM中使用EF框架生成相关数据实体与context(EF自行查阅,这里不多介绍),即M层,项目目录如下:C# 实践之 基于WPF的mvvm模型,使UI独立,逻辑可测

     

  2. 为了使V层(展示层)与核心业务控制解耦,需要在VM层实现对业务的控制,建通用Command类,代码如下:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;
    
    namespace ViewModels
    {
        public class Command :ICommand
        {
            private Action methodToExecute = null;
            private Func<bool> methodCanExecute = null;
    
            public Command(Action methodToExecute, Func<bool> methodCanExecute)
            {
                this.methodToExecute = methodToExecute;
                this.methodCanExecute = methodCanExecute;           
            }
            
            public void Execute(object parameter)
            {
                this.methodToExecute();
            }
            public bool CanExecute(object parameter)
            {
                if (this.methodCanExecute == null)
                {
                    return true;
                }
                else
                {
                    return this.methodCanExecute();
                }
            }
            
            public event EventHandler CanExecuteChanged;
            public void RaseCanExecuteChangedEvent()
            {
                if (this.CanExecuteChanged != null)
                {
                    this.CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
    }
    View Code

相关文章:

  • 2022-03-04
  • 2021-07-19
  • 2021-07-22
  • 2021-08-07
  • 2022-12-23
  • 2021-09-12
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
相关资源
相似解决方案