locked
How to get screen width and height at startup

    Question

  • I programmatically place a bunch of images on my main page at startup.  However I can't get the actualwidth and actualheight of the page until around 2-3 seconds after startup.

    I have a mainpage in XAML called this:

    Page Name="AnimalMainPage"


    In the C# behind I'll then do some maths operations like this:

    xLocation = (AnimalMainPage.ActualWidth) * xRandom;
    
    
    
    
    However I get null for the actualwidth until some unknown time afterwards.  Is there a way to get the actualwidth and actualheight of the device at startup so that I can populate the screen without having to wait some time to populate the screen?  The issue I have is that to reliably wait long enough to make sure that the app doesn't crash I am waiting 3 seconds, sometimes this is okay, but at othertimes the splash screen has already disappeared and the objects are populating on the screen after the UI is visible to the user.

    Thanks for your help

    Monday, November 03, 2014 12:34 PM

Answers

  • Using Window.Current.Bounds

      protected override void OnLaunched(LaunchActivatedEventArgs e)
            {
                Debug.WriteLine(Window.Current.Bounds.Width.ToString() + ":" + Window.Current.Bounds.Height.ToString());


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Monday, November 03, 2014 2:13 PM