MinWidth 在 MainPage.xaml (UWP) 中不起作用。

huangapple go评论56阅读模式
英文:

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&lt;800) 
        {
            Debug.WriteLine(&quot;smaller than 800&quot;);
            var targetsize = new Size(800, 600);
            ApplicationView.GetForCurrentView().TryResizeView(targetsize);
           
        }
    }

huangapple
  • 本文由 发表于 2023年2月24日 01:23:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定