我想在 WPF 中开发一个应用程序,在 Windows Phone 8.1 中也一样。我想将变量的值从 100 增加到 200,从 200 增加到 300,然后继续。这是比赛的比分。现在我想在 UI 中为其设置动画。我写了一个这样的示例代码。
<Button Click="Button_Click" >Start</Button>
<TextBlock x:Name="tbvalue" />
因此,当单击按钮时,值将从 100 到 200,但这应该在 UI 中显示,如 101,102,103 ...,并在 UI 中动画增量值。
我已经写了后面的代码
private void Button_Click(object sender, RoutedEventArgs e)
{
while (count < upperLimit)
{
count += 1;
this.Dispatcher.BeginInvoke(new Action<object>(perform), sender);
System.Threading.Thread.Sleep(100);
}
i++;
upperLimit = i * 100;
}
在那
private void perform(object obj)
{
tbvalue.Text = count.ToString();
}
但是使用这个我没有实现计数器动画。关于如何实现此功能的任何想法或建议。
最佳答案
这就是我所做的,并且工作正常。我制作了一个演示 wpf 应用程序
public MainWindow()
{
InitializeComponent();
Counter_Timer.Interval = new TimeSpan(0, 0, 1);
Counter_Timer.Tick += dispatcherTimer_Tick;
counter.Content = 100;
}
private readonly DispatcherTimer Counter_Timer = new DispatcherTimer();
private void Button_Click(object sender, RoutedEventArgs e)
{
Counter_Timer.Start();
}
public void dispatcherTimer_Tick(object sender, object e)
{
counter.Content = Convert.ToInt16(counter.Content) + 1;
if (Convert.ToInt16(counter.Content) >= 200)
{
Counter_Timer.Stop();
}
}
Xaml 文件:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>
<Button Click="Button_Click" Grid.Row="0" Name="Counter" ></Button>
<Label Name="counter" Grid.Row="1"></Label>
</Grid>
关于c# - 如何在 UI 中对计数器值进行动画处理以增加 C# WPF 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26885555/