更新时间:2021-07-08 01:44:41
ActualHeight
和 ActualWidth
直到控件被测量和排列后才设置.通常 InitializeComponent()
中没有什么会导致度量,所以当它返回时这些仍然为零.
ActualHeight
and ActualWidth
are not set until the control is measured and arranged. Usually there is nothing in InitializeComponent()
that causes a measure, so when it returns these will still be zero.
您可以通过在窗口的 InitializeComponent()
之后手动调用窗口的 Measure()
和 Arrange()
方法来强制提前计算这些方法代码>返回.
You can force these to be computed earlier by simply calling the window's Measure()
and Arrange()
methods manually after the window's InitializeComponent()
returns.
如果您要根据内容调整大小:
If you are sizing to content:
window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
window.Arrange(new Rect(0, 0, window.DesiredSize.Width, window.DesiredSize.Height));
如果您使用明确的窗口大小:
If you are using an explicit window size:
window.Measure(new Size(Width, Height));
window.Arrange(new Rect(0, 0, window.DesiredSize.Width, window.DesiredSize.Height));