Reputation: 361
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
Upvotes: 0
Views: 712
Reputation: 1072
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.
Upvotes: 1