英文:
MinWidth don't working in MainPage.xaml (UWP)
问题
在我的UWP应用程序中,我将MinWidth设置为800,但在运行时它可以变得更小。
MainPage.xaml
<Page ............
............
MinWidth="800">
</Page>
我希望我的应用程序不会小于800像素。
英文:
In my UWP app, I make the MinWidth 800, but it can get smaller when I run it.
MainPage.xaml
<Page ............
............
MinWidth="800">
</Page>
I want to my app can't get smaller than 800 pixels.
答案1
得分: 1
似乎您想设置UWP窗口的最小尺寸。通常,我们会使用ApplicationView.SetPreferredMinSize(Size) 方法 来设置UWP应用的最小尺寸。但这对于您的情况不太适用。您要求的最小宽度是800,而此API允许的最小尺寸的最大值是500 x 500。
如果您希望保持应用的首选尺寸,您可以处理窗口的sizechanged事件。当应用的大小发生变化,如果应用的宽度小于800,您可以将窗口的尺寸调整回到800。
代码示例:
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged;
}
private void CoreWindow_SizeChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
{
var newSize = args.Size;
if (newSize.Width < 800)
{
Debug.WriteLine("小于800");
var targetSize = new Size(800, 600);
ApplicationView.GetForCurrentView().TryResizeView(targetSize);
}
}
英文:
It seems that you want to set the mini size of the UWP window. In general, we will use ApplicationView.SetPreferredMinSize(Size) Method to set the mini size of the UWP app. But this is not suitable for your scenario. You are asking for a mini width as 800 and the largest allowed minimum size for this API is 500 x 500.
If you want to keep your app in a preferred size, what you could do is handle the sizechanged event of the window. When the app is resized and if the width of the app is less than 800, you could adjust the size of the window back to 800.
Code example:
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged;
}
private void CoreWindow_SizeChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
{
var newszie = args.Size;
if (newszie.Width<800)
{
Debug.WriteLine("smaller than 800");
var targetsize = new Size(800, 600);
ApplicationView.GetForCurrentView().TryResizeView(targetsize);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论