WPF MVVM下 关闭窗体 并打开新窗体

这个功能应该常用与登录 后 主界面显示

Login 窗体

Main窗体

LoginViewModel

MainViewModel

首先定义俩个帮助类

WindowBehavior 和WindowManager

   public class WindowBehavior : Behavior
    {
        /// 
        /// 关闭窗口
        /// 
        public bool Close
        {
            get { return (bool)GetValue(CloseProperty); }
            set { SetValue(CloseProperty, value); }
        }
        public static readonly DependencyProperty CloseProperty =
            DependencyProperty.Register("Close", typeof(bool), typeof(WindowBehavior), new PropertyMetadata(false, OnCloseChanged));
        private static void OnCloseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var window = ((WindowBehavior)d).AssociatedObject;
            var newValue = (bool)e.NewValue;
            if (newValue)
            {
                window.Close();
            }
        }

    }
    public static class WindowManager
    {
        private static Hashtable _RegisterWindow = new Hashtable();

        public static void Register(string key)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, typeof(T));
            }
        }

        public static void Register(string key, Type t)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, t);
            }
        }

        public static void Remove(string key)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                _RegisterWindow.Remove(key);
            }
        }

        public static void Show(string key, object VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.DataContext = VM;
                win.Show();
            }
        }
        public static void ShowDialog(string key, object VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.DataContext = VM;
                win.ShowDialog();
            }
        }
    }

 

在登录窗体里  Login窗体 增加Main窗体

    public partial class LoginView : Window
    {
        public LoginView()
        {
            InitializeComponent();

            DataContext = new LoginViewModel();
            WindowManager.Register("MainView");
        }
    }

在Login 界面上 增加行为

WPF MVVM下 关闭窗体 并打开新窗体_第1张图片

  
        
    

LoginViewModel 里增加属性 和命令

 

    
        public LoginViewModel()
        {

            CMDLogin = new DeletegateCommand();
            CMDLogin.ExecuteAction = new Action(CMDLoginExecute);

        }
/// 
        /// 是否关闭窗口 
        /// 
       private bool toClose = false;
        public bool ToClose
        {
            get => toClose;
            set
            {
                toClose = value;
                if (toClose)
                {
                    this.RaisePropertyChanged("ToClose");
                }
            }
        }


        #region 命令绑定及实现
        public DeletegateCommand CMDLogin { get; set; }
        private void CMDLoginExecute(object parameter)
        {
            //// OISGlobal.ShowMessage("Test", "Test", MSGTYPE.INFO);
            WindowManager.Show("MainView", new MainViewModel());
            ToClose = true;
        }

 

登录界面的button 绑定命令

 

你可能感兴趣的:(WPF)