【问题标题】:Trouble updating the margin property of buttons to animate them无法更新按钮的边距属性以对其进行动画处理
【发布时间】:2014-03-11 03:32:57
【问题描述】:

我有一个 WP8 应用程序,我在轮子上显示按钮。单击时,我希望轮子转动并将单击的按钮带到它的顶部。

我将按钮与它们的边距属性一起放置。我想我会使用双 while 循环来沿着圆圈逐渐移动按钮,就像这样(尽量简化它):

private void Select(Button selectedButton)
{
    Button[] SelectedButtons = { button1, button2, button3 };
    //Calculates the distance between buttons in degrees
    double buttonDistance = 360 / SelectedButtons.Length;

    double angleDeg;
    double angleRad;
    int i = 0;
    int j = 0;

    //While the selected button hasn't reach top position, move the wheel
    while (selectedButton.Margin != new Thickness(<top position of the wheel>)
    {
        //Moves the buttons of a single degree
        while (i < SelectedButtons.Length)
        {
            //Calculates the position of the current button in the array in degrees and converts it in radians
            angleDeg = j + 90 + i * buttonDistance;
            angleRad = Math.PI * angleDeg / 180;

            //Button positioning
            SelectedButtons[i].Margin = new Thickness(<calculation of the margins with the given angles>);
            i++;
        }
        System.Threading.Thread.Sleep(5);
        j++;
        i = 0;
    }
}

但是,即使使用 Sleep() 函数,按钮的位置也只会在循环结束后更新,并且不会像我想要的那样设置动画。

我尝试在循环中使用 UpdateLayout(),在 ContentPanel 和按钮本身上,我尝试了 InvalidateArrange() 和 InvalidateMeasure(),甚至添加了一个画布,但什么也没做:按钮只会更新当时间结束时,从未有过。

你们能帮帮我吗?

【问题讨论】:

  • 这是一个非常经典的案例,从 GUI 线程快速更新 UI 元素并期望它实时重绘。如果您在主线程中工作,则不会更新 GUI。这是我不久前发布的解决方案的类似问题的示例:stackoverflow.com/questions/11667053/…

标签: c# button windows-phone-8 margin transformation


【解决方案1】:

快速更新,适用于任何可能偶然发现相同问题的人:

确实,正如 steveg89 所说,您不能直接从 GUI 线程更新 UIElements。他提供的链接建议使用后台工作人员或代表来使其工作。不幸的是,WP8 也不允许这样做。

唯一的解决方案是切换到故事板动画:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206955(v=vs.105).aspx

对于绝对的初学者来说并不容易,但是当您花时间了解它们的工作原理及其不同类型(简单动画、关键帧动画或路径动画)时,它会很好地工作(但是 Windows Phone 不支持路径动画。浪费了不少时间。))

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2016-07-23
    • 2022-01-02
    相关资源
    最近更新 更多