我有一个Canvas
与TextBlock
像这样:的ActualHeight和ActualWidth的始终为零
<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>
我的应用程序是这样的,当用户浏览该网页时,TextBlock
将在Canvas
中心和如果TextBlock
的宽度比画布的更大,将发生字幕动画:
private void SetAnimation()
{
Canvas.SetLeft(WordBlock, (ContentPanel.ActualWidth - WordBlock.ActualWidth)/2);
Canvas.SetTop(WordBlock, (ContentPanel.ActualHeight - WordBlock.ActualHeight)/2);
if (WordBlock.ActualWidth > ContentPanel.ActualWidth)
{
MarqueeAnimation.From = WordBlock.ActualWidth;
MarqueeAnimation.To = -WordBlock.ActualWidth;
MarqueeAnimation.Duration = new Duration(new TimeSpan(0, 0, 10));
MarqueeBoard.Begin();
}
}
这种方法被称为的OnNavigatedTo。我不知道为什么TextBlock
不会居中,因为ActualHeight
和ActualWidth
属性总是回到0.0。我不想放置固定尺寸,因为这是一个Windows应用商店应用,并希望它可以跨不同的屏幕尺寸进行缩放。
任何想法?我卡住了。
你是如何设置WordBlock的文本? – wdavo
在上一页中,将TextBox的内容作为导航参数传递,然后从OnNavigatedTo中检索它。 – Martin