locked
Detecting Screen Size

    Question

  • I have an app in which I need to determine the size of the user's screen. I'm sure this must be a relatively simple task, but even with all the searching I have done, I can't seem to find the answer. Can anybody help me? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, May 7, 2014 8:03 PM

Answers

  • Hi Nathan,

    If your app is in full screen mode, you definitely can use Window.Current.Bounds to get the current width and height as the screen size.

    But if your app is not full screen mode, the only way to do this is to import a DX method, I have a demo I wrote several days ago, hope helps: http://1drv.ms/1nq1kHA

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, May 8, 2014 2:26 AM
    Moderator
  • Put this code in App class

    public static string CurrentSettings;

    This code must after where if stamnt end if (rootFrame == null)
    {
    }
    var info = DisplayInformation.GetForCurrentView();
    double percent = Convert.ToInt64(Regex.Match(info.ResolutionScale.ToString(), @"\d+").Value);
    var framWidth = (int)(Window.Current.Bounds.Width * percent / 100.0 + .01);
    var framHeight = (int)(Window.Current.Bounds.Height * percent / 100.0 + .01);
    CurrentSettings = string.Format("{0:0000}{1:0000}{2}", framWidth, framHeight, percent);


    Agha Khan

    Friday, May 9, 2014 5:50 AM
  • Check out the following. It's in VB, but easily adaptable to C#. It shows how to get the current width (you shouldn't really need to get the height as Windows Store apps always use the full height). The sample code also shows how to adjust the screen layout to adapt to *changes* is screen/app size.

    Changing Windows Store App Size Blog Post

    NOTE: Windows 8.1 only...I don't think this will work in 8.0


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.


    Friday, May 9, 2014 6:20 AM

All replies

  • Hi Nathan,

    If your app is in full screen mode, you definitely can use Window.Current.Bounds to get the current width and height as the screen size.

    But if your app is not full screen mode, the only way to do this is to import a DX method, I have a demo I wrote several days ago, hope helps: http://1drv.ms/1nq1kHA

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, May 8, 2014 2:26 AM
    Moderator
  • At Build 2014 a presentation is given by Peter Torr From 4 to 40 inches: Developing Windows Applications across Multiple Form Factors

    In it he shows how he's rearranging controls based on the device layout - specifically he shows this for the phone

    <local:LayoutRulesBasedPage.LayoutRules>
      <local:ShapeLayoutRule ShapeType="Wide"/>
      <local:ShapeLayoutRule ShapeType="Tall"/>
      <local:ShapeLayoutRule ShapeType="Square"/>
    </local:LayoutRulesBasedPage.LayoutRules>

    And for the Windows main page he shows this

    <local:LayoutRulesBasedPage.LayoutRules>
      <local:WidthLayoutRule MinWidth="1200" VisualStateName="Wide"/>
      <local:WidthLayoutRule MinWidth="700" VisualStateName="Square"/>
      <local:WidthLayoutRule MinWidth="400" VisualStateName="Thin"/>
      <local:DefaultLayoutRule VisualStateName="Rail"/>
    </local:LayoutRulesBasedPage.LayoutRules>

    He said that the full code will be posted after the conference - where might we find the full example and see it's implementation?

    Thank you

    Thursday, May 8, 2014 7:09 PM
  • Put this code in App class

    public static string CurrentSettings;

    This code must after where if stamnt end if (rootFrame == null)
    {
    }
    var info = DisplayInformation.GetForCurrentView();
    double percent = Convert.ToInt64(Regex.Match(info.ResolutionScale.ToString(), @"\d+").Value);
    var framWidth = (int)(Window.Current.Bounds.Width * percent / 100.0 + .01);
    var framHeight = (int)(Window.Current.Bounds.Height * percent / 100.0 + .01);
    CurrentSettings = string.Format("{0:0000}{1:0000}{2}", framWidth, framHeight, percent);


    Agha Khan

    Friday, May 9, 2014 5:50 AM
  • Check out the following. It's in VB, but easily adaptable to C#. It shows how to get the current width (you shouldn't really need to get the height as Windows Store apps always use the full height). The sample code also shows how to adjust the screen layout to adapt to *changes* is screen/app size.

    Changing Windows Store App Size Blog Post

    NOTE: Windows 8.1 only...I don't think this will work in 8.0


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.


    Friday, May 9, 2014 6:20 AM
  • Hey Nathan,

    It really was this easy:

    void GetSize()
    {
         width = Window.Current.Bounds.Width;
         height = Window.Current.Bounds.Height;
    }

    then I just called it everywhere to react to any change of any kind:

    MainPage_OrientationChanged(DisplayInformation sender, object args)
    DisplayInformation_DisplayContentsInvalidated(DisplayInformation sender, object args)
    DisplayPropertiesOnOrientationChanged(object sender)
    Current_SizeChanged

    Friday, May 9, 2014 2:42 PM