关闭时,MahApps.Metro窗口会导致错误

时间:2018-06-09 10:55:00

标签: wpf window mahapps.metro

我将我的应用程序升级到MahApps.Metro但是我得到了一个非常模糊的错误。这是我的代码:

public partial class Login : MetroWindow
{
    public bool AllowAutomaticLogin { get; set; }

    public Login()
    {
        InitializeComponent();
        Loaded += OnLoaded;
        AllowAutomaticLogin = true;
    }

    public Login(bool allowAutomaticLogin)
    {
        InitializeComponent();
        Loaded += OnLoaded;
        AllowAutomaticLogin = allowAutomaticLogin;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        DataContext = new LoginViewModel(CloseWindow, AllowAutomaticLogin);
    }

    private void CloseWindow(object obj)
    {
        GetWindow(this).Close();
    }

    ...other stuff that doesn't matter
}

在我的视图模型中,我有构造函数:

public LoginViewModel(Action<object> closeWindow, bool allowAutomaticLogin)
    {
        AllowAutomaticLogin = allowAutomaticLogin;
        CloseWindow = closeWindow;
        Login = new RelayCommand(_Login);
        SwitchAccounts = new RelayCommand(_SwitchAccounts);
        if (AllowAutomaticLogin)
        {
            try
            {
                AttendanceAppServiceClient proxy = new AttendanceAppServiceClient();
                if (proxy.IsRegistered(Environment.UserName))
                {
                    Username = Environment.UserName;
                    IsRecognized = true;
                    RecognizedText = "You have been recognized as " + Username + ". Please provide your password.";

                    if (Properties.Settings.Default.AutomaticLogin)
                    {
                        var user = proxy.GetUser(Username);
                        var mainWindow = new MainWindow(user);
                        mainWindow.Show();
                        CloseWindow(null);
                    }
                }
                else
                {
                    IsRecognized = false;
                }
                proxy.Close();
            }
            catch (Exception ex)
            {

            }
        }
    }

关键是如果PC用户名对应于数据库中的有效用户名,并且应用程序设置允许,则只记录它们(只需关闭登录窗口并打开主窗口)。现在,如果设置为false,您可以像往常一样登录并填写像这样的用户名和密码

private void _Login(object obj)
    {
        try
        {
            AttendanceAppServiceClient proxy = new AttendanceAppServiceClient();
            User user;
            if ((user = proxy.IsValidUser(Username, SecurePassword)) != null)
            {
                var mainWindow = new MainWindow(user);
                mainWindow.Show();
                CloseWindow(null);
            }
            else
            {
                MessageBox.Show("Invalid credentials. Try again.");
            }
            proxy.Close();
        }
        catch (Exception ex)
        {

        }
    }

基本上这段代码:

var mainWindow = new MainWindow(user);
mainWindow.Show();
CloseWindow(null);

是一样的。但是如果我让视图构造函数中的AllowAutomaticLogin设置为true,它就会给我这个

System.ArgumentNullException: Value cannot be null. Parameter name: window

但如果我将其设置为false并像往常一样登录,则通过_Login方法并关闭并正确打开新窗口。堆栈跟踪是:

at System.Windows.Interop.WindowInteropHelper..ctor(Window window)
at ControlzEx.Behaviors.WindowChromeBehavior.AssociatedObject_SourceInitialized(Object sender, EventArgs e) in C:\projects\controlzex\src\ControlzEx\Behaviors\WindowChromeBehavior.cs:line 323
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Window.OnSourceInitialized(EventArgs e)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at AttendanceApp.App.Main()

为了清楚起见,在我添加MetroWindow之前它完全正常(我所做的只是将基类更改为MetroWindow并添加控件:MetroWindow而不是Window)。可能是什么问题。

0 个答案:

没有答案