zoukankan      html  css  js  c++  java
  • WPF 介绍一种在MVVM模式下弹出子窗体的方式

    主要是通过一个WindowManager管理类,在window后台代码中通过WindowManager注册需要弹出的窗体类型,在ViewModel通过WindowManager的Show方法,显示出来。

    WindowManager代码如下:

     public static class WindowManager
        {
            private static Hashtable _RegisterWindow = new Hashtable();
    
            public static void Regiter<T>(string key)
            {
                _RegisterWindow.Add(key, typeof(T));
            }
            public static void Regiter(string key, Type t)
            {
                if (!_RegisterWindow.ContainsKey(key))
                    _RegisterWindow.Add(key, t);
            }
    
            public static void Remove(string key)
            {
                if (_RegisterWindow.ContainsKey(key))
                    _RegisterWindow.Remove(key);
            }
    
            public static void ShowDialog(string key, object VM)
            {
                if (!_RegisterWindow.ContainsKey(key))
                {
                    throw (new Exception("没有注册此键!"));
                }
    
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.DataContext = VM;
                win.ShowDialog();
            }
    
        }

    做一个扩展方法,将子窗体注册方法扩展到Window类型的对象上。

       public static class WindowExt
        {
            public static void Register(this Window win, string key)
            {
                WindowManager.Regiter(key, win.GetType());
            }
    
            public static void Register(this Window win,string key,Type t)
            {
                WindowManager.Regiter(key,t);
            }
    
            public static  void Register<T>(this Window win, string key)
            {
                WindowManager.Regiter<T>(key);
            }
        }

    添加一个ViewModelBase,并在类中添加ShowDialog方法,这样所有继承的ViewModel都有这个方法

        public class ViewModelBase
        {
            public void ShowDialog(string key,object vm)
            {
                WindowManager.ShowDialog(key,vm);
            }
    
            public void ShowMessage(string mes,string title="",MessageBoxButton buttons= MessageBoxButton.OK)
            {
                MessageBox.Show(mes,title,buttons);
            }
        }

    添加一个窗体,并注册子窗体, this.Register<Window1>("Window1");

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new MainWindowViewModel();
                this.Register<Window1>("Window1");
            }
        }

    添加ViewModel,继承自ViewModelBase,并在对应的命令中弹出子窗体Window1

        public class MainWindowViewModel:ViewModelBase
        {
            public MainWindowViewModel()
            {
                BtnCommand = new DelegateCommand(ExecuteBtn);
            }
    
            public DelegateCommand BtnCommand { get; set; }
    
    
            private void ExecuteBtn()
            {
                ShowDialog("Window1",this);
            }
    
        }

    这样子窗体就弹出来了。

  • 相关阅读:
    看完这篇,网络面试稳了!
    Python 单元测试详解
    聊一聊,Python自动化测试框架
    测试妹纸说,你这用了几年的postman,只用了它的皮毛
    Win系统设置Apache Tomcat开机后台自动启动
    .Net Framework中的委托与事件——热水器事例
    Unity3d学习清单
    python 进制、ASCII码转换
    python正则 re 模块函数
    mysql 简单手工注入
  • 原文地址:https://www.cnblogs.com/czly/p/9640033.html
Copyright © 2011-2022 走看看