UWP应用MainPage.xaml中MinWidth设置不生效问题求助
解决UWP窗口最小宽度限制问题
你给Page设置MinWidth="800"仅对页面控件本身生效,不会限制应用窗口的整体尺寸。要实现窗口宽度不小于800像素,需要直接设置应用窗口的最小尺寸,具体操作如下:
方法一:在App.xaml.cs中设置窗口最小尺寸
在App.xaml.cs的OnLaunched方法里,添加代码指定窗口的最小宽高:
protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { // 可在此添加之前状态的恢复逻辑 } Window.Current.Content = rootFrame; } if (e.PrelaunchActivated == false) { if (rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e.Arguments); } // 设置窗口最小尺寸 var appView = ApplicationView.GetForCurrentView(); appView.SetPreferredMinSize(new Windows.Foundation.Size(800, 600)); // 第二个参数为最小高度,可按需调整 Window.Current.Activate(); } }
方法二:在MainPage后台代码中设置
若想在页面加载时设置,可在MainPage.xaml.cs的Loaded事件中添加代码:
private void Page_Loaded(object sender, RoutedEventArgs e) { var appView = ApplicationView.GetForCurrentView(); appView.SetPreferredMinSize(new Windows.Foundation.Size(800, 600)); }
注意事项
- UWP系统对窗口最小尺寸有默认限制,最小宽度不能低于500像素、最小高度不能低于320像素(不同系统版本可能略有差异),若设置值低于该限制,系统会自动使用最低允许值。
- 设置完成后,用户无法将窗口缩小到指定的最小尺寸以下。
内容的提问来源于stack exchange,提问作者MinecraftWolf
相关产品推荐
相关产品推荐

