locked
How to get width and height of Monitor in Metro App? RRS feed

  • Question

  • How to get width and height of Monitor in Metro App?
    Wednesday, February 29, 2012 9:27 AM

Answers

  • An easy way is to ask it to your main page in the Load event:

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        var w = this.LayoutRoot.ActualWidth;
        var h = this.LayoutRoot.ActualHeight;
    }

    • Marked as answer by Jie Bao Tuesday, March 6, 2012 5:02 PM
    Wednesday, February 29, 2012 10:25 AM
  • You should call your Init method in the Loaded event of the page.

    • Proposed as answer by Jie Bao Thursday, March 1, 2012 5:59 AM
    • Marked as answer by Jie Bao Tuesday, March 6, 2012 5:02 PM
    Thursday, March 1, 2012 5:56 AM

All replies

  • An easy way is to ask it to your main page in the Load event:

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        var w = this.LayoutRoot.ActualWidth;
        var h = this.LayoutRoot.ActualHeight;
    }

    • Marked as answer by Jie Bao Tuesday, March 6, 2012 5:02 PM
    Wednesday, February 29, 2012 10:25 AM
  • But, i get the result is zero in this way. 

    The code is as follows:

    XMAL:

    <Grid x:Name="XLayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

            <TextBlock  Height="148"  Width="572" FontSize="30" x:Name="XText"/>

        </Grid>

    c#:

    private void Init() {
            XText.Text = "Width: " + this.XLayoutRoot.ActualWidth + "  Height: " + this.XLayoutRoot.ActualHeight;
    }

     this.XLayoutRoot.ActualWidth  and this.XLayoutRoot.ActualHeight are zero.

    Thursday, March 1, 2012 2:02 AM
  • When do you call the Init() method in your code? Do you call it after InitializeComponent() of your UserControl?

    Please note, that Main Page size will often be the same with the monitor size, but not when the app is snapped or filled.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 1, 2012 3:07 AM
  • public sealed partial class BlankPage : Page
        {
            public BlankPage()
            {
                this.InitializeComponent();
                Init();
            }


            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }


            private void Init() {
                XText.Text = "Width: " + this.XLayoutRoot.ActualWidth + "  Height: " + this.XLayoutRoot.ActualHeight;
            }


        }

      this.XLayoutRoot.ActualWidth  and this.XLayoutRoot.ActualHeight are zero.


    • Edited by Roy_Rock Thursday, March 1, 2012 5:27 AM
    Thursday, March 1, 2012 5:26 AM
  • Hi Roy Rock,

    Please get the width and height when page loaded.

    after load event, the page can be launch, so this time the page can have width and height.


    Thanks! Damon.Tian

    Thursday, March 1, 2012 5:55 AM
  • You should call your Init method in the Loaded event of the page.

    • Proposed as answer by Jie Bao Thursday, March 1, 2012 5:59 AM
    • Marked as answer by Jie Bao Tuesday, March 6, 2012 5:02 PM
    Thursday, March 1, 2012 5:56 AM
  • thank you!
    Tuesday, November 6, 2012 1:52 AM