Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVVM灯光WPF打开新窗口_Wpf_Mvvm Light - Fatal编程技术网

MVVM灯光WPF打开新窗口

MVVM灯光WPF打开新窗口,wpf,mvvm-light,Wpf,Mvvm Light,我是MVVM新手,并通过MVVM Light学习它 我在wpf中有一个带有登录窗口的应用程序。当用户输入正确的凭据时,登录窗口应关闭,新的主窗口应打开。登录部分已经开始工作,但是如何打开新窗口并关闭当前窗口(login.xaml) 此外,还必须为此新的主窗口提供一些参数 有人能告诉我正确的方向或提供一些信息吗?因为您使用的是MvvmLight,所以您可以使用Messenger类(MvvmLight中的帮助器类),该类用于在ViewModels之间以及ViewModels与视图之间发送消息(通知+

我是MVVM新手,并通过MVVM Light学习它

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

此外,还必须为此新的主窗口提供一些参数


有人能告诉我正确的方向或提供一些信息吗?

因为您使用的是MvvmLight,所以您可以使用
Messenger
类(MvvmLight中的帮助器类),该类用于在ViewModels之间以及ViewModels与视图之间发送消息(通知+对象),在您的情况下,当登录在
LoginViewModel
中成功时(可能在提交按钮的处理程序中),您需要向
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
和该
OtherWindowViewModel
之间发送对象,但这次需要发送对象,而不仅仅是
NotificationMessage
: 在LoginView模型中:

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

这是我通常用来关闭登录窗口并显示主应用程序窗口的代码的可能重复:谢谢。我拿着它却没有东西!
 Messenger.Default.Send<YourObjectType>(new YourObjectType(), "Message");
Messenger.Default.Register<YourObjectType>(this, "Message", (yourObjectType) =>
                                                           //use it 
                                                             );