gpt4 book ai didi

c# - 实际高度/实际宽度

转载 作者:行者123 更新时间:2023-11-30 20:06:29 25 4
gpt4 key购买 nike

我对 ActualWidthActualHeight 的工作方式或计算方式有点困惑。

<Ellipse Height="30" Width="30" Name="rightHand" Visibility="Collapsed">
<Ellipse.Fill>
<ImageBrush ImageSource="Images/Hand.png" />
</Ellipse.Fill>
</Ellipse>

当我使用上面的代码时,ActualWidthActualHeight 得到 30。但是当我以编程方式定义椭圆时,ActualWidthActualHeight 为 0,即使我定义了 (max)height 和 (max)width 属性 - 我不明白怎么会是0呢?

最佳答案

ActualWidthActualHeight 是在调用 MeasureArrange 之后计算的。

WPF 的布局系统在将控件插入可视树后自动调用它们(在 DispatcherPriority.Render 恕我直言,这意味着它们将排队等待执行并且结果不会立即可用).
您可以通过在 DispatcherPriority.Background 中排队执行操作或手动调用这些方法来等待它们可用。

调度程序变体示例:

Ellipse ellipse = new Ellipse();

ellipse.Width = 150;
ellipse.Height = 300;

this.grid.Children.Add(ellipse);

this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
MessageBox.Show(String.Format("{0}x{1}", ellipse.ActualWidth, ellipse.ActualHeight));
}));

显式调用示例:

Ellipse ellipse = new Ellipse();

ellipse.Width = 150;
ellipse.Height = 300;

ellipse.Measure(new Size(1000, 1000));
ellipse.Arrange(new Rect(0, 0, 1000, 1000));

MessageBox.Show(String.Format("{0}x{1}", ellipse.ActualWidth, ellipse.ActualHeight));

关于c# - 实际高度/实际宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9689513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com