-
Notifications
You must be signed in to change notification settings - Fork 712
Description
In a WinUI 3 application, when the window’s restore button is disabled and the app is minimized and then reopened, the UI appears to shift downward.
Steps to reproduce:
- Create a WinUI 3 window.
- Disable the restore button and resizing.
- Run the app.
- Minimize the window.
- Reopen it.
Expected behavior:
The UI layout should remain unchanged when minimized and reopened.
Actual behavior:
The entire UI content shifts downward after reopening.
I have two pages. The main page includes a button that navigates to the second page. When navigating, the second page should display in full window mode, and the restore button is disabled. However, when the app is minimized and then reopened, the UI appears shifts downward. What could be causing this problem?
MainWindow.xaml
<Window x:Class="App1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="App1">
<Frame x:Name="ContentFrame" />
</Window>
MainWindow.xaml.cs
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
namespace App1
{
public sealed partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ContentFrame.Navigated += ContentFrame_Navigated;
ContentFrame.Navigate(typeof(MainPage), this);
}
private void ContentFrame_Navigated(object sender, Microsoft.UI.Xaml.Navigation.NavigationEventArgs e)
{
var appWindow = GetAppWindowForCurrentWindow();
if (appWindow != null && appWindow.Presenter is OverlappedPresenter presenter)
{
if (e.SourcePageType == typeof(SecondPage))
{
// On second page: maximize and disable resizing
presenter.Maximize();
presenter.IsMaximizable = false;
presenter.IsResizable = false;
}
else
{
// On main page: enable resizing
presenter.IsMaximizable = true;
presenter.IsResizable = true;
}
}
}
public void NavigateToSecondPage()
{
ContentFrame.Navigate(typeof(SecondPage));
}
public AppWindow? GetAppWindowForCurrentWindow()
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(windowId);
}
}
}
MainPage.xaml
<Page x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Button x:Name="OpenSecondPageBtn"
Content="Open Second Screen"
Click="OpenSecondPageBtn_Click"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="200" />
</Page>
MainPage.xaml.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
namespace App1
{
public sealed partial class MainPage : Page
{
private MainWindow? _mainWindow;
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Parameter is MainWindow mainWindow)
{
_mainWindow = mainWindow;
}
}
private void OpenSecondPageBtn_Click(object sender, RoutedEventArgs e)
{
_mainWindow?.NavigateToSecondPage();
}
}
}
SecondPage.xaml
<Page x:Class="App1.SecondPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Spacing="20">
<TextBlock Text="Second Screen"
FontSize="32"
FontWeight="Bold"
HorizontalAlignment="Center" />
<TextBlock Text="Window is maximized and cannot be resized"
FontSize="16"
HorizontalAlignment="Center"
Foreground="{ThemeResource SystemFillColorAttentionBrush}" />
<Button x:Name="BackBtn"
Content="Back to Main Screen"
Click="BackBtn_Click"
Width="200"
HorizontalAlignment="Center"
Margin="0,20,0,0" />
</StackPanel>
</Grid>
</Page>
SecondPage.xaml.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace App1
{
public sealed partial class SecondPage : Page
{
public SecondPage()
{
InitializeComponent();
}
private void BackBtn_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
}
}
After (App is minimized then reopened)

