There are two (it would seem) idiomatic ways to minimize a WPF window in code:
window.WindowState = window.WindowState.Minimized
SystemCommands.MinimizeWindow(window)
The desired behavior is that the windows 11 window minimize animation is seen. The actual behavior is that the window instantly disappears from the screen.
Please note: This is for a window with a custom chrome - therefore triggering the behavior from code seems necessary:
<Window
WindowStyle="None"
AllowTransparency="True
Background="Transparent">
<!-- custom windows chrome here -->
</Window>
Is this a defect in the WPF desktop pack for .NET 5 (and 6)?
How does one not get the desired behavior without reverting back to default windows with standard chrome? It is not an option to use a WinUI 3.0 window since XAML islands have not been released yet and gradual modernization is needed.
There are some proposed solutions on the web, like:
And the other (old) stackoverflow question:
However, I could not get it to work with a window with a transparent background.
===== update after issue was closed =====
Reviewer: please read this carefully before voting to close the issue again and point to exact workable solution.
Issues with existing answers:
First suggestion is to change WindowStyle, then WindowState; this poses a problem since it requires AllowTransparency=false which defeats the purpose of the exercise here - which is getting a custom chrome.
Other suggestion is to use Win32 API's. Again I tried to do that but it did not work in my setting. I haven't logged the exact error messages I got though. However, looking at the code again there is an obvious issue with it: it has the same window requirement as the first suggestion: the window must not be transparent - observe code from the old answer:
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == ApiCodes.WM_SYSCOMMAND) { if (wParam.ToInt32() == ApiCodes.SC_MINIMIZE) { WindowStyle = WindowStyle.SingleBorderWindow; WindowState = WindowState.Minimized; handled = true; } else if (wParam.ToInt32() == ApiCodes.SC_RESTORE) { WindowState = WindowState.Normal; WindowStyle = WindowStyle.None; handled = true; } } return IntPtr.Zero; }