UWP:边界不 return 实际屏幕分辨率

UWP : Bounds doesn't return actual Screen Resolution

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

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

     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)。但我的要求是获得实际分辨率,即由 user.Please 设置的 1600 x 900 指导我如何实现。

谢谢

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

下面是我的测试代码:

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 布局,可以参考 MSDN 联机帮助 adaptive UI