在 Windows 10 中检测到布局循环,但在 Windows 8.1 中未检测到

Layout Cycle Detected in Windows 10 but not in Windows 8.1

我正在使用一个计时器,它每 10 秒更新一次布局,直到进度条未完全填满。

在Windows 8.1 中似乎没有问题,但在Windows 10 中,抛出 Layout Cycle Detected Exception。

原代码是

private void SetVerticalOffSet()
        {
            Popup ParentPopup = this.Parent as Popup;

            if (ParentPopup == null)
                return;

            double ActualVerticalOffset = ParentPopup.VerticalOffset;
            double ActualHorizontalOffset = ParentPopup.HorizontalOffset;

            double NewVerticalOffset = (Window.Current.Bounds.Height - DecryptionDialog_MainGrid.ActualHeight) / 2;
            double NewHorizontalOffset = (Window.Current.Bounds.Width - DecryptionDialog_MainGrid.ActualWidth) / 2;
            if (ActualVerticalOffset != NewVerticalOffset || ActualHorizontalOffset != NewHorizontalOffset)
            {
                ParentPopup.VerticalOffset = NewVerticalOffset;
                ParentPopup.HorizontalOffset = NewHorizontalOffset;
            }
        }

每次更新布局时都会调用它。

if 块导致无限循环,因为 ActualVerticalOffSet 和 ActualHorizo​​ntalOffSet 在 Windows 10 中不断变化。(小数点后第 10 位)

if (ActualVerticalOffset != NewVerticalOffset || ActualHorizontalOffset != NewHorizontalOffset)
        {
            ParentPopup.VerticalOffset = NewVerticalOffset;
            ParentPopup.HorizontalOffset = NewHorizontalOffset;
        }

此修复的暂定解决方案是

if (Math.Round(ActualVerticalOffset) != Math.Round(NewVerticalOffset) ||     Math.Round(ActualHorizontalOffset) != Math.Round(NewHorizontalOffset))
{
        ParentPopup.VerticalOffset = NewVerticalOffset;
        ParentPopup.HorizontalOffset = NewHorizontalOffset;
}

直到微软出现