c# - 如何在 UI 中对计数器值进行动画处理以增加 C# WPF 中的值?

标签 c# .net wpf multithreading wpf-animation

我想在 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/

相关文章:

c# - NSubstitute 与 PRISM EventAggregator : Assert that calling a method triggers event with correct payload

c# - 为什么我们使用@Master类型?

c# - 创建一个方形边框,内部有图像,宽度和高度取决于统一的网格列

c# Json.net DbGeography 反序列化错误

c# - 在类库中嵌入 exe 文件

.net - 使用 monotorrent : How do I add webseeds? 创建一个 torrent 文件

c# - 如何使用访问 token 和刷新 token 从 google api 访问用户日历列表和事件

c# - 将 DateTime 转换为指定格式

c# - 当未选择行时,复选框列第一次不会更改其状态

c# - 如何使用 MVVM Light ViewModelLocator 处理多个 ViewModel