Get Screen Resolution in Win10 UWP App

前端 未结 7 2324
刺人心
刺人心 2020-11-28 08:54

As an UWP App runs in window mode on common desktop systems the \"old\" way of getting the screen resolution won\'t work anymore.

Old Resolution with Window.C

7条回答
  •  情话喂你
    2020-11-28 09:22

    Call this method anywhere, anytime (tested in mobile/desktop App):

    public static Size GetCurrentDisplaySize() {
        var displayInformation = DisplayInformation.GetForCurrentView();
        TypeInfo t = typeof(DisplayInformation).GetTypeInfo();
        var props = t.DeclaredProperties.Where(x => x.Name.StartsWith("Screen") && x.Name.EndsWith("InRawPixels")).ToArray();
        var w = props.Where(x => x.Name.Contains("Width")).First().GetValue(displayInformation);
        var h = props.Where(x => x.Name.Contains("Height")).First().GetValue(displayInformation);
        var size = new Size(System.Convert.ToDouble(w), System.Convert.ToDouble(h));
        switch (displayInformation.CurrentOrientation) {
        case DisplayOrientations.Landscape:
        case DisplayOrientations.LandscapeFlipped:
            size = new Size(Math.Max(size.Width, size.Height), Math.Min(size.Width, size.Height));
            break;
        case DisplayOrientations.Portrait:
        case DisplayOrientations.PortraitFlipped:
            size = new Size(Math.Min(size.Width, size.Height), Math.Max(size.Width, size.Height));
            break;
        }
        return size;
    }
    

提交回复
热议问题