UWP不同尺寸页面导航显示异常问题技术咨询
Hey there! Let's break down your UWP page sizing questions one by one:
First off: No, you absolutely don't have to keep all pages the same size! UWP is designed to support dynamic window sizing, so your issue is just that the window isn't adjusting to fit Page2's required dimensions when you navigate.
Here's how to fix this and implement different page sizes properly:
1. Resize the window before navigating to Page2
You can adjust the app window's size right before triggering the navigation from Page1. For example, in a button click event:
private async void GoToPage2Btn_Click(object sender, RoutedEventArgs e) { // Get the current app window instance var currentWindow = ApplicationView.GetForCurrentView(); // Resize to Page2's desired 1000×800 (units are effective pixels, DPI-aware) bool resizeSuccess = await currentWindow.TryResizeView(new Size(1000, 800)); // Navigate only if resize worked (optional check) if (resizeSuccess) { Frame.Navigate(typeof(Page2)); } }
This ensures the window is already the right size when Page2 loads, so all its content will be visible.
2. Let Page2 handle its own window size
Alternatively, you can set the window size when Page2 loads, so no matter where you navigate from, it will adjust automatically. Add this to Page2's code-behind:
protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); var currentWindow = ApplicationView.GetForCurrentView(); // Force the window to 1000×800 on page load await currentWindow.TryResizeView(new Size(1000, 800)); }
This is great if Page2 always needs that specific size, regardless of the previous page.
3. Key Notes to Keep in Mind
- Windows have system-enforced minimum sizes (usually around 500×320), so
TryResizeViewwill returnfalseif you try to go smaller than that. Always check the return value if you need to handle failures. - If Page2's content can adapt to different sizes, combine window resizing with adaptive layout techniques: use containers like
GridorRelativePanel, andVisualStateManagerto rearrange content based on window width/height. This makes your app more flexible overall.
Relevant UWP Resources to Explore
You can find deep dives on these topics in the official UWP developer docs:
- ApplicationView Class: Covers all window management features, including resizing, state changes (maximize/minimize), and window preferences.
- Adaptive Layout for UWP: Learn how to build pages that respond smoothly to different window sizes, even if you don't fix the window to a specific dimension.
- Frame Navigation Lifecycle: Understand when to trigger window changes during navigation events for the best user experience.
内容的提问来源于stack exchange,提问作者Alfonso Rodriguez

