Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询组件的最佳位置';大小_C#_Windows 8_Microsoft Metro - Fatal编程技术网

C# 查询组件的最佳位置';大小

C# 查询组件的最佳位置';大小,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,在页面的构造函数中,我尝试查询其组件大小 public sealed partial class MainPage : Page { public MainPage() { // Get the canvas size. double height = canvas.ActualHeight; double width = canvas.ActualWidth; } } 所有我得到的高度和宽度都是0。我可以知道查询组件大小

页面
的构造函数中,我尝试查询其组件大小

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        // Get the canvas size.
        double height = canvas.ActualHeight;
        double width = canvas.ActualWidth;
    }
}

所有我得到的高度和宽度都是0。我可以知道查询组件大小的最佳位置吗?

on导航到每次导航到页面时都会触发get。因此,可能需要在其中执行此操作。

当UI在屏幕上显示一次时,实际宽度和实际高度将可用。你想在哪里使用这些高度和宽度?是的。那么,一旦UI显示在屏幕上,将触发哪个页面的方法?因此,我可以重写该方法,并在其中执行大小查询。没有这样的事件。你需要让它变得棘手。你能从GUI中触发一些触发器吗?将
加载的
事件添加到你的
画布
并在那里执行。当元素被构造并添加到对象树中时,这将是触发事件的最佳位置。
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.Loaded += MainPage_Loaded;
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        // Get the canvas size.
        double height = canvas.ActualHeight;
        double width = canvas.ActualWidth;
    }
}