ptf
ptf

Reputation: 3370

Get window width / height in Windows store apps

I currently have a basic page which loads, and I need some way of obtaining the width and height of the window, preferably in the constructor. The problem is, in the constructor, or before the page is completely loaded, I can't seem to get hold of the width and height. After it is loaded I can just use:

this.ActualWidth;
this.ActualHeight;

Is there any window load complete event I can use or any way to obtain the width and height during the loading?

Upvotes: 6

Views: 15537

Answers (2)

Mike Boula
Mike Boula

Reputation: 276

You can find out the size of the Window at any moment using the property Window.Current.Bounds. For more details, read: How to get the resolution of screen? For a WinRT app?

Upvotes: 7

N_A
N_A

Reputation: 19897

Here is a blog post on how to handle the SizeChanged event. You can't get the ActualWidth/ActualHeight without doing something like this because they are calculated when the control is rendered.

private void OnWindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
    var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value;
    double AppWidth = e.Size.Width;
    double AppHeight = e.Size.Height;

    // DownloadImage requires accurate view state and app size!
    DownloadImage(CurrentViewState, AppHeight, AppWidth);
}

Window.Current.SizeChanged += OnWindowSizeChanged;

Upvotes: 3

Related Questions