UWP (WinRT) Get Raw and Real Resolution

Working with resolutions can be a real pain with WinRT apps. The MS framework isn’t as far developed as we wish sometimes. Getting the “real” resolution on a device depends on several factors.

  1. Does the device supports software buttons
  2. Do we display the titlebar
  3. Which scale factor is currently active
  4. The orientation of the device
  5. DPI

All in all it can be a little bit tricky to get the desired value out of the framework.

Feel free to use or modify my helper class to handle those issues.

using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;

namespace YOUR_NAMESPACE
{
    public static class ScreenValues
    {
        /// <summary>
        /// The scalefactor for the resolution.
        /// </summary>
        public static double ScaleFactor { get { return DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; } }

        /// <summary>
        /// The logical DPI.
        /// </summary>
        public static float DPI { get { return DisplayInformation.GetForCurrentView().LogicalDpi; } }

        /// <summary>
        /// The raw screen resolution.
        /// </summary>
        public static Rect VisibleBounds { get { return ApplicationView.GetForCurrentView().VisibleBounds; } }

        /// <summary>
        /// The screen resolution with scaling applied.
        /// </summary>
        public static Size ScreenResolution { get { return new Size(VisibleBounds.Width * ScaleFactor, VisibleBounds.Height * ScaleFactor); } }

        /// <summary>
        /// The total screen resolution with scaling applied, including the notificationbar.
        /// </summary>
        public static Size TotalScreenResolution { get { return CalculateTotalScreenResolution(); } }

        /// <summary>
        /// The width of the notificationBar.
        /// </summary>
        public static double NotificationBarWidth { get { return CalculateNotificationBarSize().Width; } }

        /// <summary>
        /// The height of the notificationBar.
        /// </summary>
        public static double NotificationBarHeight { get { return CalculateNotificationBarSize().Height; } }

        /// <summary>
        /// The current orientation of the device.
        /// </summary>
        public static DisplayOrientations CurrentDisplayOrientation { get { return DisplayInformation.GetForCurrentView().CurrentOrientation; } }

        private static Size CalculateNotificationBarSize()
        {
            var test = CoreApplication.GetCurrentView().TitleBar;
            if (CurrentDisplayOrientation == DisplayOrientations.Landscape)
                return new Size(VisibleBounds.Left * ScaleFactor, ScreenResolution.Height);
            else if (CurrentDisplayOrientation == DisplayOrientations.LandscapeFlipped)
                return new Size(CoreApplication.GetCurrentView().TitleBar.Height * ScaleFactor, ScreenResolution.Height);
            return new Size(ScreenResolution.Width, VisibleBounds.Top * ScaleFactor);
        }

        private static Size CalculateTotalScreenResolution()
        {
            if(CurrentDisplayOrientation == DisplayOrientations.Landscape || CurrentDisplayOrientation == DisplayOrientations.LandscapeFlipped)
                return new Size(ScreenResolution.Width + NotificationBarWidth, ScreenResolution.Height);
            return new Size(ScreenResolution.Width, ScreenResolution.Height + NotificationBarHeight);
        }
    }
}

 

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.