Xamarin UWP 应用程序不限制用户将 window 的大小调整为 150(推荐)分辨率的较小尺寸
Xamarin UWP app not restricting the user from resizing the window to lower size in 150(recommended) resolution
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文档。
The smallest allowed minimum size is 192 x 48 effective pixels. The largest allowed minimum size is 500 x 500 effective pixels. If you set a value outside of these bounds, it is coerced to be within the allowed bounds. (To learn about effective pixels, see Introduction to Windows app design.)
因此,请将您的最大宽度从 150 修改为 192。
private void Button_Click(object sender, RoutedEventArgs e)
{
var size = new Size(192, 150);
ApplicationView.GetForCurrentView().SetPreferredMinSize(size);
ApplicationView.GetForCurrentView().TryResizeView(size);
}
如果要将当前windows尺寸设置为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
The requested size is larger than the available work area (no effect);
1920*1080 Scale150Percent,可用工作区为 1280 * (720 - taskbarheight)。当您设置 600 * 800 时,TryResizeView
无效。因为该应用程序的 window 比当前工作量更大。请尝试设置为600 * 600。
更新 1
now its not allowing the user to shrink or restore down below 600,600. But the issue now is, its not allowing to maximize or expand more than 600,600 as well.
请添加最大化 window 宽度触发条件,如下所示。
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
}
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文档。
The smallest allowed minimum size is 192 x 48 effective pixels. The largest allowed minimum size is 500 x 500 effective pixels. If you set a value outside of these bounds, it is coerced to be within the allowed bounds. (To learn about effective pixels, see Introduction to Windows app design.)
因此,请将您的最大宽度从 150 修改为 192。
private void Button_Click(object sender, RoutedEventArgs e)
{
var size = new Size(192, 150);
ApplicationView.GetForCurrentView().SetPreferredMinSize(size);
ApplicationView.GetForCurrentView().TryResizeView(size);
}
如果要将当前windows尺寸设置为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
The requested size is larger than the available work area (no effect);
1920*1080 Scale150Percent,可用工作区为 1280 * (720 - taskbarheight)。当您设置 600 * 800 时,TryResizeView
无效。因为该应用程序的 window 比当前工作量更大。请尝试设置为600 * 600。
更新 1
now its not allowing the user to shrink or restore down below 600,600. But the issue now is, its not allowing to maximize or expand more than 600,600 as well.
请添加最大化 window 宽度触发条件,如下所示。
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
}