为什么在 UWP 的 Icon1(UserControl) 中隐藏 TextBlock 后 ActualWidth 没有改变?

Why ActualWidth don't changed after hiding TextBlock in Icon1(UserControl) in UWP?

隐藏Icon1中的TextBlock后如何获取actualWidth? 为什么隐藏Label后Width1和Width2都一样?隐藏 TextBlock(标签)后,我想要 Icon1 的 ActualWidth。 根据 ActualWidth 更改可见性后,我需要做一些操作!!

我还有一个问题。即,在隐藏标签的可见性之前,有什么方法可以确定 Icon1 的实际宽度(隐藏标签后 Icon1 的宽度是多少)??

我的完整Project Link.

Why both Width1 and Width2 are Same after hide the Label?

如果您使用调试写入线打印 Width2,您会发现它会在下一个 SizeChanged 时更新,问题是您在 UI 更新之前获得 Width2 值,它将 return 上一个价值。请在 width2 之前设置任务延迟,您将获得更新值 46。

await Task.Delay(100);
double width2 = icon1.ActualWidth;

Before hiding the visibility of label ,is there any way to determine the actualWidth.

正如您上面提到的,您可以将 ActualWidth 属性 调用为 return 宽度值。

更新

bool isResize; //set it as true after Label hidden.
private void Icon1_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (isResize)
    {
        var newSize = e.NewSize;
       // do your action here.
    }

}