UWP:Bounds不返回实际屏幕分辨率

时间:2015-11-13 05:51:10

标签: windows screen-resolution uwp

我正在开发一个UWP应用程序,我必须得到用户设置的实际屏幕分辨率(例如1600 x 900)。我在stackoverflow上经历了许多类似的问题,在我的代码中尝试了它们,但没有人可以提供帮助。

要获得当前的屏幕分辨率,我有以下代码:

     var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
     var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
     var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
     Height = size.Height;
     Width = size.Width;

我的桌面分辨率设置为1600 x 900.使用此代码,我得到高度为828,宽度为1600(即1600 x 828)。但我的要求是获得用户设定的实际分辨率,即1600 x 900.请指导我如何实现这一目标。

谢谢

1 个答案:

答案 0 :(得分:1)

为了获得实际的分辨率,你需要调用 ApplicationView.GetForCurrentView()。真正显示VisibleBoundsbefore 窗口。更好的地方是在Window.Current.Activate()方法之后的App.xaml.cs中。 这个post非常清楚地解释了启动窗口的大小。

以下是我的测试代码:

In App.xaml.cs:
           //here set preferred size = 800*800 for test
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
            ApplicationView.PreferredLaunchViewSize = new Size(800, 800);

            Window.Current.Activate();
           //here to get the real full screen size
            var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
            var full = ApplicationView.GetForCurrentView().IsFullScreen;
            var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
            var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);

in my Mainpage.xaml.cs:
        //here the size should be 800 * 800
        private void Button_Click(object sender, RoutedEventArgs e)
        {
           var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
           var full = ApplicationView.GetForCurrentView().IsFullScreen;
           var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
           var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
       }

此外,关于您的问题,如何处理桌面和平板电脑模式,您能否澄清一下您想要处理的情况?如果您想根据不同的屏幕分辨率调整UI布局,可以参考有关adaptive UI的MSDN在线帮助。

相关问题