如何解决Xamarin UWP 应用程序不限制用户以 150推荐分辨率将窗口大小调整为较小的大小
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 分辨率下处理屏幕尺寸。
解决方法
请检查 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 - 任务栏高度)。当您设置 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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。