Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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#_Xaml_Windows Runtime_Actualwidth_Actualheight - Fatal编程技术网

C# 对象的实际高度和实际宽度始终为零

C# 对象的实际高度和实际宽度始终为零,c#,xaml,windows-runtime,actualwidth,actualheight,C#,Xaml,Windows Runtime,Actualwidth,Actualheight,我有一个Canvas和一个TextBlock类似: <Canvas x:Name="ContentPanel" Grid.Row="1" DoubleTapped="ContentPanel_DoubleTapped"> <TextBlock x:Name="WordBlock" FontSize="226.667" FontFamily="Segoe UI Semilight" TextAlignment="Center"

我有一个
Canvas
和一个
TextBlock
类似:

<Canvas x:Name="ContentPanel" Grid.Row="1" DoubleTapped="ContentPanel_DoubleTapped">
        <TextBlock x:Name="WordBlock" FontSize="226.667" FontFamily="Segoe UI Semilight" TextAlignment="Center"
                   RenderTransformOrigin="0.5, 0.5">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="translate"/>
            </TextBlock.RenderTransform>
        </TextBlock>
    </Canvas>
此方法称为OnNavigatedTo。我不明白为什么
TextBlock
不会居中,因为
ActualHeight
ActualWidth
属性总是返回0.0。我不想设置固定大小,因为这是一个Windows应用商店应用程序,希望它可以跨不同的屏幕大小进行扩展


有什么想法吗?我被卡住了。

当调用OnNavigatedTo时,我不相信页面实际上已经绘制好了。我在尝试调整大小和重新排列事物时也有类似的困惑。答案似乎是等待页面加载完毕,然后进行计算:

public ChallengePage()
{
  this.InitializeComponent();
  this.Loaded += ChallengePage_Loaded;
}

void ChallengePage_Loaded(object sender, RoutedEventArgs e)
{
  *Do your calculations which use ActualWidth and ActualHeight in here*
}

我相信你需要这样的东西。将加载的处理程序添加到页面的初始化中,然后可以从中调用SetAnimation。

如何设置WordBlock的文本?在上一页中,将文本框的内容作为导航参数传递,然后从OnNavigatedTo中检索它。因此,我将使用OnNavigatedTo检索我传递到此页面的单词,并将其设置为WordBlock的值,然后在加载时调整大小并重新排列所有内容?是的,在导航时对文本进行更改,让页面加载以便它实际呈现对象,然后,它们应该有大小,以便您可以在加载时重新排列:o)
public ChallengePage()
{
  this.InitializeComponent();
  this.Loaded += ChallengePage_Loaded;
}

void ChallengePage_Loaded(object sender, RoutedEventArgs e)
{
  *Do your calculations which use ActualWidth and ActualHeight in here*
}