How to properly enable dpi-scaling on uwp app

假装没事ソ 提交于 2019-12-05 12:34:20

Windows 8.1 doesn't have a concept of 125% scaling; it uses 100% / 140% / 180%. The value of DisplayInformation.ResolutionScale will be 100. So it's actually scaled by 80% (100 / 125). And 44 * 0.8 is ~35.

Note: The following is not supported since it relies on reflection and using features of Windows 10 inside a Windows 8.1 app. Use at your own risk.

Here's one way you can try and give people on Windows 10 a better experience with your Windows 8.1 app. I briefly tested it on Windows 10 but you would want to do more thorough testing on Windows 8.1 as well:

private void FixUpScale()
{
  // Need to add a using for System.Reflection;

  var displayInfo = DisplayInformation.GetForCurrentView();
  var displayType = displayInfo.GetType();
  var rawPixelsProperty = displayType.GetRuntimeProperty("RawPixelsPerViewPixel");

  if (rawPixelsProperty != null)
  {
    var realScale = (double)rawPixelsProperty.GetValue(displayInfo);

    // To get to actual Windows 10 scale, we need to convert from 
    // the de-scaled Win8.1 (100/125) back to 100, then up again to
    // the desired scale factor (125). So we get the ratio between the
    // Win8.1 pixels and real pixels, and then square it. 
    var fixupFactor = Math.Pow((double)displayInfo.ResolutionScale /
      realScale / 100, 2);

    Window.Current.Content.RenderTransform = new ScaleTransform
    {
      ScaleX = fixupFactor, 
      ScaleY = fixupFactor 
    };
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!