【发布时间】: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