Get window width / height in Windows store apps

落花浮王杯 提交于 2019-12-07 03:22:52

问题


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?


回答1:


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?




回答2:


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;


来源:https://stackoverflow.com/questions/12899121/get-window-width-height-in-windows-store-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!