- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 TextBlock
的 Canvas
,如下所示:
<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
' s宽度大于 Canvas 宽度,会出现跑马灯动画:
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 应用商店应用,我希望它可以在不同的屏幕尺寸上进行缩放。
有什么想法吗?我卡住了。
最佳答案
当调用 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*
}
我相信您会需要类似的东西。将 Loaded 处理程序添加到页面的初始化中,然后您可以从那里调用 SetAnimation。
关于c# - 对象的 ActualHeight 和 ActualWidth 始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12766891/
我正在尝试在窗口显示之前计算 StackPanel width, height(位于网格的中间单元格)(例如窗口构造函数)。如何实现?
我在运行时创建了一个 StackPanel,我想像这样测量 StackPanel 的 Height: StackPanel panel = new StackPanel(); panel.Childr
我尝试创建一个具有半透明圆形背景的自定义控件: 问题是我可能无法绑定(bind)到 ActualHeight/ActualWidth 属性,因为它们不是依赖项。 如何使矩形和文
我有一个带有 TextBlock 的 Canvas,如下所示: 我
不,我没有像其他许多人一样将 ActualHeight 设置为零的问题。在我的 Windows 应用商店应用程序 (WinRT) 中,我在调试器中看到了这一点: ActualHeight | 50.7
我需要一个 Viewport3D仅用于使用 Petzold.Media3D.ViewportInfo 进行几何计算.我宁愿不必将它放在 Window 中。或以其他方式呈现它。 我试图通过实例化 Vie
我有两个单独的 ItemsControl s 并排出现。 ItemsControl s 绑定(bind)到同一个 ItemsSource ,但它们以不同的方式显示数据。 左侧显示的每个项目很可能比右侧
我想将 WPF 路径添加到 InkCanvas并使用选择来选择 WPF 路径。 所以,我使用这个代码。 System.Windows.Shapes.Path path = drawCanvas.Chi
最近我遇到了一个看似简单的问题:我想在我正在处理的一个应用程序中使用 Mode=OneWayToSource 将 Canvas 的 Width 和 Height 参数推送到 ViewModel 中。原
我在 Canvas 中有一个 Grid ,定义如下:
我想知道我的 StackPanel 所有项目的高度。 有什么区别: Height - 获取或设置元素的建议高度。 ActualHeight - 获取该元素的渲染高度。 (只读) ExtentHeigh
我是一名优秀的程序员,十分优秀!