我开始在UWP中使用多个窗口,并且需要显示辅助窗口的屏幕中心或至少父窗口的中心。
除了Window.Current.Bounds
属性之外,我发现与如何指定在屏幕上显示其他窗口的位置无关。
这是我用来创建其他窗口的方法的简化版本。方法签名为:CreateFrameWindow(Size size, Type pageType, object parameter)
CoreApplicationView newWindow = CoreApplication.CreateNewView();
ApplicationView newView = null;
bool result = await newWindow.Dispatcher.TryRunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(pageType, parameter);
Window.Current.Content = frame;
Window.Current.Activate();
newView = ApplicationView.GetForCurrentView();
});
result = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newView.Id);
newView.TryResizeView(size);
该TryResizeView
作品很好,只要二级窗口有足够的空间基础上,屏幕上的当前位置来调整。我想启用调整到最大可用大小(最大化时的窗口大小)并将其放置在屏幕中央。如果无法做到这一点,则可以将其放置在父窗口或主窗口的中心。
显示UWP主窗口的辅助窗口中心
CoreApplicationView
不提供用于手动设置视图位置的api。根据您的要求,请尝试AppWindow
用于存档此功能。并AppWindow
具有RequestMoveRelativeToDisplayRegion
将窗口定位在指定显示区域中指定偏移处的方法。有关更多信息,请参考官方代码示例场景5
更新资料
如果要使新窗口显示在中间,则需要事先知道窗口大小,并为RequestMoveRelativeToDisplayRegion方法计算XY值。
X = (1920-W)/2 //1920 is Horizontal Resolution W is the new window's width
Y = (1080-H)/2 //1080 is Vertical Resolution H is the new window's height
要获得当前的显示分辨率,请参考此案例链接
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width*scaleFactor, bounds.Height*scaleFactor);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句