我在WPF窗口中得到一些非常奇怪的行为,我可以使用基本应用程序重新创建。当我将窗口从最大化状态最小化时,然后恢复我的窗口它不会返回最大化。我已经实现了代码来尝试和解决这个问题,但窗口仍然会返回到WindowState.Normal
。
需要注意的一件重要事情是,只有当我独立运行应用程序时才会发生这种情况。如果我在visual studio中以调试模式运行,一切都按预期工作。此外,这种行为发生在多个盒子上,而不仅仅是我自己的。
使用以下代码重现的步骤:
单击任务栏中的应用程序
预期结果:应用程序窗口返回最大化状态
实际结果:应用程序窗口处于已恢复状态,WindowState
= WindowState.Normal
以下是演示窗口的XAML代码。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowState="Maximized"
Activated="MainWindow_OnActivated"
StateChanged="MainWindow_OnStateChanged">
<Grid>
</Grid>
</Window>
和代码隐藏:
public partial class MainWindow : Window
{
private WindowState _lastWindowState;
private bool _activating;
public MainWindow()
{
InitializeComponent();
_lastWindowState = WindowState.Normal;
}
private void MainWindow_OnActivated(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
WindowState = _lastWindowState;
}
}
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized)
{
_lastWindowState = WindowState;
}
}
}
在我的实际应用程序中,我在WindowState
函数和OnActivated
函数中设置StateChanged
之前和之后放置了日志代码。从最小化激活后,我得到以下内容:
OnActivated 1 - _lastWindowState: Maximized
OnStateChanged - _lastWindowState: Maximized
OnActivated 2 - _lastWindowState: Maximized
OnStateChanged - _lastWindowState: Normal
最后,如果这有帮助,这是最后一次StateChanged调用中的Environment.StackTrace
:
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at MapViewer.MainWindow.MainWindow_OnStateChanged(Object sender, EventArgs e) in c:\APP\MainWindow.xaml.cs:line 994
at System.Windows.Window.OnStateChanged(EventArgs e)
at System.Windows.Window.WmSizeChanged(IntPtr wParam)
at System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(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 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, 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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
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.Threading.Dispatcher.Run()
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 MapViewer.App.Main() in c:\Projects\APP\obj\x86\Debug\App.g.cs:line 0