Wpf MahApps.Metro窗口关闭时出错

Wpf MahApps.Metro窗口关闭时出错,wpf,window,mahapps.metro,Wpf,Window,Mahapps.metro,我将我的应用程序升级到MahApps.Metro,但我得到了一个非常模糊的错误。这是我的密码: public partial class Login : MetroWindow { public bool AllowAutomaticLogin { get; set; } public Login() { InitializeComponent(); Loaded += OnLoaded; AllowAutomaticLo

我将我的应用程序升级到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)
            {

            }
        }
    }
基本上这段代码:

var mainWindow = new MainWindow(user);
mainWindow.Show();
CloseWindow(null);
都一样。但是如果我让视图构造函数中的AllowAutomaticLogin设置为true,它会给出以下结果

System.ArgumentNullException: Value cannot be null. Parameter name: window
但是如果我将其设置为false并像往常一样登录,in将通过_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)。可能有什么问题。

为什么不在
CloseWindow
方法中调用
this.Close()
?我得到了相同的结果,我尝试了。似乎它只是不会关闭地铁窗口,除非加载完成,或者我猜,因为我还没有修复它,我只是在它上面贴了绷带,但这将有助于解决问题。为什么你要在加载之前关闭窗口?好吧,如果满足某个条件,我不再需要这个窗口,我只需打开一个新窗口,然后关闭这个窗口。对于普通windows,它可以正常工作,而对于MetroWindow,它不能……请提供您的问题的回购:
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()