MVVM Light WPF打开新窗口 [英] MVVM Light WPF open new window

查看:921
本文介绍了MVVM Light WPF打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVVM的新手,可以通过MVVM Light学习它.

我在wpf中有一个带有登录窗口的应用程序.当用户输入正确的凭据时,登录窗口应关闭,新的MainWindow应打开.登录部分已经可以使用,但是如何打开新窗口并关闭当前窗口(login.xaml)?

还必须为此新MainWindow提供一些参数.

任何人都可以把我带到正确的方向或向我提供一些信息吗?

解决方案

由于使用的是MvvmLight,因此可以使用Messenger类(mvvmlight中的帮助器类),该类用于在ViewModel之间发送消息(通知+对象).在ViewModels和Views之间,如果您在LoginViewModel中登录成功(可能在Submit Button的处理程序中),则需要向LoginWindow发送一条消息以关闭自身并显示其他窗口:

LogInWindow后面的代码

public partial class LogInWindow: Window
{       
    public LogInWindow()
    {
        InitializeComponent();
        Closing += (s, e) => ViewModelLocator.Cleanup();

        Messenger.Default.Register<NotificationMessage>(this, (message) =>
        {
            switch (message.Notification)
            {
                case "CloseWindow":
                    Messenger.Default.Send(new NotificationMessage("NewCourse"));
                    var otherWindow= new OtherWindowView();
                    otherWindow.Show();   
                    this.Close();            
                    break;
            } 
        }
    }
 }

,并在LogInViewModel的SubmitButonCommand中发送该结束消息:

private RelayCommand _submitButonCommand;
public RelayCommand SubmitButonCommand
{
    get
    {
        return _closeWindowCommand
            ?? (_closeWindowCommand = new RelayCommand(
            () => Messenger.Default.Send(new NotificationMessage("CloseWindow"))));
    }
}

并使用相同的方法在LoginViewModelOtherWindowViewModel之间发送对象,但是这次您需要发送对象而不只是NotificationMessage: 在LoginViwModel中:

 Messenger.Default.Send<YourObjectType>(new YourObjectType(), "Message");

并在OtherWindowViewModel中接收该对象:

Messenger.Default.Register<YourObjectType>(this, "Message", (yourObjectType) =>
                                                           //use it 
                                                             );

I am new to MVVM and learning it with MVVM Light.

I have an application in wpf with a login window. When the user enters correct credentials the login window should close and a new MainWindow should open. The part of the login is already working but how can I open a new window and close the current (login.xaml)?

Also some parameters must be given to this new MainWindow.

Can anyone put me in the correct direction or provide me some info?

解决方案

since you are using MvvmLight you could use the Messenger Class (a helper class within mvvmlight) which is used to send messages (notification + objects) between ViewModels and between ViewModels and Views, in your case when the login succeeded in the LoginViewModel (probably in the handler of the Submit Button) you need to send a message to the LoginWindow to close itself and show that other windows :

LogInWindow code behind

public partial class LogInWindow: Window
{       
    public LogInWindow()
    {
        InitializeComponent();
        Closing += (s, e) => ViewModelLocator.Cleanup();

        Messenger.Default.Register<NotificationMessage>(this, (message) =>
        {
            switch (message.Notification)
            {
                case "CloseWindow":
                    Messenger.Default.Send(new NotificationMessage("NewCourse"));
                    var otherWindow= new OtherWindowView();
                    otherWindow.Show();   
                    this.Close();            
                    break;
            } 
        }
    }
 }

and for in the SubmitButonCommandat the LogInViewModel (for example) send that closing Message:

private RelayCommand _submitButonCommand;
public RelayCommand SubmitButonCommand
{
    get
    {
        return _closeWindowCommand
            ?? (_closeWindowCommand = new RelayCommand(
            () => Messenger.Default.Send(new NotificationMessage("CloseWindow"))));
    }
}

and use the Same approach to send Object between LoginViewModel and that OtherWindowViewModel with the exception that this time you need to send Objects instead of just NotificationMessage : in the LoginViwModel:

 Messenger.Default.Send<YourObjectType>(new YourObjectType(), "Message");

and to receive that object in the OtherWindowViewModel :

Messenger.Default.Register<YourObjectType>(this, "Message", (yourObjectType) =>
                                                           //use it 
                                                             );

这篇关于MVVM Light WPF打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆