0
votes

I am developing a UWP application and I have to get the actual screen resolution that user has set(e.g. 1600 x 900). I went through many similar questions on stackoverflow, tried them in my code but none could help.

To get the current screen resolution, I have the following code:

     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;

My Desktop Resolution is set to 1600 x 900. Using this code I get the Height as 828 and width as 1600 (i.e. 1600 x 828). But my requirement is to get actual resolution i.e. 1600 x 900 that is set by the user.Please guide me how can I achieve that.

Thanks

1
Have you accounted for the taskbar at the bottom of the screen? If you have it pinned, you don't get to recover that real-estate. 72 pixels sounds suspiciously familiar.Robert Harvey
@RobertHarvey I have not accounted for the Task Bar. It was pretty easy to get actual Screen Resolution in WPF(using PrimaryScreenHeight, etc...) but not in UWP. Also this value of 72 will change on different resolutions (As my application is for Tablets). Thanks for pointing out the Taskbar but user may or may not Pin the Task Bar. How do I get around with it (Getting the Task Bar Height)?Saurabh Rai
Have you tried this?Robert Harvey
@RobertHarvey Yes!! As I already mentioned, I have tried similar questions on Stack OverFlow.This was one of the methods I tried. This yields the same result (1600 x 828).Saurabh Rai
@RobertHarvey One more Update !! I tried the running same code after switching to Tablet Mode on my desktop and now the resolution it gives is 1600 x 860. I am confused how to handle the Desktop and Tablet Modes?Saurabh Rai

1 Answers

1
votes

In order to get the actual resolution, you need to call ApplicationView.GetForCurrentView().VisibleBoundsbefore window is really displayed. The better place is in App.xaml.cs just after the Window.Current.Activate() method. This post explains the start up window size very clearly.

Below is my test code:

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);
       }

Besides, as about your question, how to handle the desktop and tablet mode, could you clarify which situation you want to handle? If you want to adapt the UI layout based on different screen resolution, you can refer to MSDN online help about adaptive UI.