简体   繁体   English

验证用户时关闭登录对话框?

[英]Closing the login dialog when user is verified?

Pretty new at MVVM, so excuse the clumsiness of my code... MVVM 很新,所以请原谅我的代码笨拙......

My program (App.xaml.cs) starts out by opening the main window (MainView)我的程序(App.xaml.cs)首先打开主 window(MainView)

        protected override void OnStartup(StartupEventArgs e)
    {            
        Window window = new MainView();
        window.DataContext = new MainViewModel();
        window.WindowStyle = WindowStyle.None;
        window.ResizeMode = ResizeMode.NoResize;
        window.Show();

        base.OnStartup(e);
    }

and the constructor of MainView starts out by opening the login dialog (LoginView) MainView 的构造函数通过打开登录对话框(LoginView)开始

        public MainView()
    {
        var login = new LoginView();
        login.DataContext = new LoginViewModel();
        DataContext = new MainViewModel();
        login.ShowDialog();
        InitializeComponent();
    }

In my LoginViewModel (this is probably the wrong place for it, but I'll be refactoring to be more MVVM compliant later on) I verify the password在我的 LoginViewModel 中(这可能是错误的地方,但我稍后会重构以更符合 MVVM)我验证密码

            if (user.PasswordHash == newHash)
        {
            MyGlobals.userLoggedIn = user.UserName;
            ShowMessageBox("Hurra.");
        }

and when that's done I want to close the dialog, which would leave me with the MainView again - I've tried just about everything I can think of, but... my inexperience is definitely holding me back.完成后,我想关闭对话框,这将使我再次使用 MainView - 我已经尝试了几乎所有我能想到的东西,但是......我的经验不足肯定会让我退缩。

Any ideas on how I can approach this?关于如何解决这个问题的任何想法? Google hasn't been that much of a help so far...到目前为止,谷歌并没有提供太多帮助......

The MVVM purest way of closing a View from its ViewModel is via an attached property.从 ViewModel 中关闭 View 的 MVVM 最纯粹的方法是通过附加属性。

public static class perWindowHelper
{
    public static readonly DependencyProperty CloseWindowProperty = DependencyProperty.RegisterAttached(
        "CloseWindow",
        typeof(bool?),
        typeof(perWindowHelper),
        new PropertyMetadata(null, OnCloseWindowChanged));

    private static void OnCloseWindowChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
    {
        if (!(target is Window view))
        {
            return;
        }

        if (view.IsModal())
        {
            view.DialogResult = args.NewValue as bool?;
        }
        else
        {
            view.Close();
        }
    }

    public static void SetCloseWindow(Window target, bool? value)
    {
        target.SetValue(CloseWindowProperty, value);
    }

    public static bool IsModal(this Window window)
    {
        var fieldInfo = typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic);
        return fieldInfo != null && (bool)fieldInfo.GetValue(window);
    }
}

Usage is用法是

<Window x:Class= ...
    vhelp:perWindowHelper.CloseWindow="{Binding ViewClosed}">

which is bound to a corresponding bool?哪个绑定到相应的bool? property on your ViewModel, that you set to true when you want to close the View. ViewModel 上的属性,当您想要关闭视图时将其设置为 true。

More details on my blog post .更多详情请参阅我的博文

You can use a NuGet package called MvvmDialogs for all dialog related operations.您可以使用名为 MvvmDialogs 的 NuGet package进行所有与对话框相关的操作。 The one capable of opening modal dialogs is called ShowDialog .能够打开模式对话框的一种称为ShowDialog You can see an example of it here .你可以在这里看到一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM