Search code examples
windows-phone-8.1uwpwindows-10-mobile

How to detect that WP8.1 app launched on Windows 10 Mobile?


I need to check OS version (WP 8.1 or W10) in my code of WP8.1 application. What better way to do this? May be reflection or some special API for this purpose?


Solution

  • I didn't find any other way to do this, so here's my approach.

    The following property IsWindows10 detects if a Windows 8.1 or Windows Phone 8.1 app is running on a Windows 10 (including Windows 10 Mobile) device.

     #region IsWindows10
    
        static bool? _isWindows10;
        public static bool IsWindows10 => (_isWindows10 ?? (_isWindows10 = getIsWindows10Sync())).Value;
    
        static bool getIsWindows10Sync()
        {
            bool hasWindows81Property = typeof(Windows.ApplicationModel.Package).GetRuntimeProperty("DisplayName") != null;
            bool hasWindowsPhone81Property = typeof(Windows.Graphics.Display.DisplayInformation).GetRuntimeProperty("RawPixelsPerViewPixel") != null;
    
            bool isWindows10 = hasWindows81Property && hasWindowsPhone81Property;
            return isWindows10;
        }
     #endregion
    

    How does it work?

    In Windows 8.1 the Package class has a DisplayName property, which Windows Phone 8.1 doesn't have. In Windows Phone 8.1 the DisplayInformation class has a RawPixelsPerViewPixel property, which Windows 8.1 doesn't have. Windows 10 (including Mobile) has both properties. That's how we can detect which OS the app is running on.