Get Screen Resolution in Win10 UWP App

前端 未结 7 2308
刺人心
刺人心 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:10

    Okay so the Answer from Juan Pablo Garcia Coello lead me to the Solution - Thanks for that!

    You can use

    var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
    

    but you must call it before the windows is displayed in my case right after

    Window.Current.Activate();
    

    is a good place. At this time you will get the bounds of the window on which your app will appear.

    Thanks a lot for help me solving it :)

    Regards Alex

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-28 09:25

    Use this method to get the screen size:

    public static Size GetScreenResolutionInfo() 
    { 
        var applicationView = ApplicationView.GetForCurrentView(); 
        var displayInformation = DisplayInformation.GetForCurrentView(); 
        var bounds = applicationView.VisibleBounds; 
        var scale = displayInformation.RawPixelsPerViewPixel; 
        var size = new Size(bounds.Width * scale, bounds.Height * scale); 
        return size; 
    } 
    

    You should call this method from App.xaml.cs, after Window.Current.Activate(); in OnLaunched method.

    Here's the sample code and you can download the full project.

    0 讨论(0)
  • 2020-11-28 09:27

    The more simple way:

    var displayInformation = DisplayInformation.GetForCurrentView();
    var screenSize = new Size(displayInformation.ScreenWidthInRawPixels, 
                              displayInformation.ScreenHeightInRawPixels);
    

    This does not depends on current view size. At any time it returns real screen resolution.

    0 讨论(0)
  • 2020-11-28 09:30

    Just set a name for main Grid or Page and call its width or height for an element you want:

    Element.Height = PagePane.Height;
    Element.width = PagePane.Width;
    

    it's the easiest way you can use!

    0 讨论(0)
  • 2020-11-28 09:32

    To improve the other answers even a bit more, the following code also takes care of scaling factors, e.g. for my 200% for my Windows display (correctly returns 3200x1800) and 300% of the Lumia 930 (1920x1080).

    var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
    var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
    var size = new Size(bounds.Width*scaleFactor, bounds.Height*scaleFactor);
    

    As stated in the other answers, this only returns the correct size on Desktop before the size of root frame is changed.

    0 讨论(0)
提交回复
热议问题