Xamarin UWP 应用程序不限制用户在 150(推荐)分辨率下将窗口大小调整为较小的大小

Posted

技术标签:

【中文标题】Xamarin UWP 应用程序不限制用户在 150(推荐)分辨率下将窗口大小调整为较小的大小【英文标题】:Xamarin UWP app not restricting the user from resizing the window to lower size in 150(recommended) resolution 【发布时间】:2021-04-08 06:22:36 【问题描述】:
     protected override void OnLaunched(LaunchActivatedEventArgs e)
             
              
             Window.Current.SizeChanged += Current_SizeChanged;
            
    


    private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        
            var displayInformation = DisplayInformation.GetForCurrentView();
            if (displayInformation.ResolutionScale == ResolutionScale.Scale125Percent || displayInformation.ResolutionScale == ResolutionScale.Scale100Percent)
            
                var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                                          displayInformation.ScreenHeightInRawPixels);
                var minheight = screenSize.Height * 0.7;
                var minwidth = screenSize.Width * 0.7;
                if (e.Size.Height < minheight || e.Size.Width < minwidth)
                
                    ApplicationView.GetForCurrentView().TryResizeView(new Size(minwidth, minheight));
                
            
    else
    //150 and above resolution
var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                           displayInformation.ScreenHeightInRawPixels);
            var minwidth = screenSize.Width / 1.5;
                 if (e.Size.Width < minwidth)
                
                    var size = new Size(1100, 640);
                    ApplicationView.GetForCurrentView().SetPreferredMinSize(size);
                    Debug.WriteLine("resize staus: " + ApplicationView.GetForCurrentView().TryResizeView(size));
                

           
        

以上代码在 100 和 125 分辨率下运行良好。但在 150(推荐)分辨率下,用户可以将应用程序最小化到更小的尺寸。知道如何处理 150 分辨率的屏幕尺寸。

【问题讨论】:

@Nico Zhu - MSFT 我正在以不同的分辨率进行测试。我不太关心硬编码,因为 var size = new Size(600, 640)(而不是找到可用的工作区);发布我将把它作为接受的帖子。谢谢!.. 最大化窗口时,可以通过 ApplicationView.GetForCurrentView().VisibleBounds. and the height of current window is the max work area height (656.66). but it can't work for TryResizeView 方法获得 VisibleBounds。并且经过测试允许最大高度为 640.66。 @NicoZhu-MSFT 我认为没关系。但是今天有一个奇怪的问题,上面的代码在所有机器的调试模式下都可以正常工作,但是当我在发布模式下构建时,它允许我调整宽度小于最小值。不知道哪里出了问题,怎么调试。 那么,您在发布模型中使用的最小尺寸是多少? @NicoZhu-MSFT 我更新了我的代码。由于宽度为 600,我的 UI 变得扭曲,我增加到 1100 宽度(因为我们有 1280,600 的可用工作区域)。它在调试模式下完美运行,但在发布时无法正常运行。 【参考方案1】:

请查看this文档。

允许的最小尺寸为 192 x 48 有效像素。允许的最大最小尺寸为 500 x 500 有效像素。如果您设置的值超出这些范围,则会强制将其设置在允许的范围内。 (要了解有效像素,请参阅 Windows 应用设计简介。)

所以,请将您的最大宽度从 150 修改为 192。

private void Button_Click(object sender, RoutedEventArgs e)

   
var size = new Size(192, 150);
ApplicationView.GetForCurrentView().SetPreferredMinSize(size);
ApplicationView.GetForCurrentView().TryResizeView(size);


如果要将当前窗口大小设置为 150*150,请将ApplicationViewMode 设置为CompactOverlay,然后将其设置为CustomSize,如下所示

var preferences = ViewModePreferences.CreateDefault(ApplicationViewMode.CompactOverlay);
preferences.CustomSize = new Windows.Foundation.Size(150, 150);
await ApplicationView.GetForCurrentView().TryEnterViewModeAsync(ApplicationViewMode.CompactOverlay, preferences);

更新

请查看此评论part

请求的大小大于可用的工作区(无效);

1920*1080 Scale150Percent,可用工作区为 1280 * (720 - taskbarheight)。设置 600 * 800 时,TryResizeView 无效。因为应用程序的窗口比当前的工作要大。请尝试设置为 600 * 600。

更新 1

现在它不允许用户缩小或恢复到 600,600 以下。但现在的问题是,它也不允许最大化或扩展超过 600,600。

请添加最大化窗口宽度触发条件,如下所示。

var displayInformation = DisplayInformation.GetForCurrentView();
if (displayInformation.ResolutionScale == ResolutionScale.Scale125Percent || displayInformation.ResolutionScale == ResolutionScale.Scale100Percent)

    var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                              displayInformation.ScreenHeightInRawPixels);
    var minheight = screenSize.Height * 0.7;
    var minwidth = screenSize.Width * 0.7;
    if (e.Size.Height < minheight || e.Size.Width < minwidth)
    
        ApplicationView.GetForCurrentView().TryResizeView(new Size(minwidth, minheight));
    

else

    var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                                displayInformation.ScreenHeightInRawPixels);
    var minwidth = screenSize.Width / 1.5;
    if (e.Size.Width < minwidth)
    
        var size = new Size(600, 640);         
        ApplicationView.GetForCurrentView().TryResizeView(size);
    
    //150 resolution

【讨论】:

感谢您的回复。为了更清楚,我在我的问题中添加了更多代码。我想限制用户将窗口屏幕的大小调整为低于 600,800 大小。目前我正在全尺寸启动应用程序(不是全屏模式,因为我不想隐藏我的任务栏)。一旦用户单击最小化或缩小应用程序窗口大小,我需要将他限制到大约 600,800 大小。 好的。知道了,请查看此备注part请求的大小大于可用工作区(无效); 1920*1080 Scale150可用工作区百分比为 1280*720。设置600*800时,TryResizeView无效。 @ Nico Zhu - MSFT 谢谢!...现在它不允许用户缩小或恢复到 600,600 以下。但现在的问题是,它也不允许最大化或扩展超过 600,600。 请检查更新1部分,并添加触发条件。

以上是关于Xamarin UWP 应用程序不限制用户在 150(推荐)分辨率下将窗口大小调整为较小的大小的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Xamarin.UWP 应用程序中实现 LongPress?

如何使用 Xamarin Forms 更改键盘的习惯用法

在 Xamarin.Forms UWP 应用程序中拦截鼠标后退按钮

Xamarin 和 UWP 辅助窗口大小调整

使用 PlatformConfiguration (Xamarin.Forms 2.3.3) 在 UWP 中将工具栏移动到底部

如何在 Visual Studio 2019 中使用 Xamarin Forms(UWP) 和 C# 创建 Web 应用程序