6

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?

ptf
  • 3,210
  • 8
  • 34
  • 67
  • 1
    They aren't calculated until the control is rendered. I don't know off-hand if there is a way to get them in code in metro. In WPF I think you can just handle the `OnResize` event or some such. – N_A Oct 15 '12 at 17:48
  • possible duplicate of [How to get the resolution of screen? For a WinRT app?](http://stackoverflow.com/questions/10828179/how-to-get-the-resolution-of-screen-for-a-winrt-app) – Robert MacLean Oct 03 '14 at 13:42
  • @ptf you can set the preferred launch size if that helps – Matthias Herrmann Mar 05 '16 at 00:06

2 Answers2

7

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?

Community
  • 1
  • 1
Mike Boula
  • 276
  • 1
  • 4
  • I'm was also relying on this value. But I'm getting weird values. When I select 10.6" 1366 x 768 these the Bounds are set to: (0, 0, 1371.429, 771.429). So it appears as if the width and height are increased by 3.5 pixels!? This is also happening on the actual device (Surface HD) – Joris Weimar Jul 01 '13 at 13:00
3

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;
N_A
  • 19,799
  • 4
  • 52
  • 98