如何在UI中设置计数器值的动画以增加C#WPF中的值?

如何在UI中设置计数器值的动画以增加C#WPF中的值?,c#,.net,wpf,multithreading,wpf-animation,C#,.net,Wpf,Multithreading,Wpf Animation,我想在WPF和WindowsPhone8.1中开发一个应用程序。我想把一个变量的值从100增加到200,从200增加到300,然后继续。这是比赛的比分。现在我想在UI中设置动画。我已经编写了这样的示例代码 <Button Click="Button_Click" >Start</Button> <TextBlock x:Name="tbvalue" /> 但是使用这个我并没有实现反动画。任何关于如何实现此功能的想法或建议。这就是我所做的,它运行良好。我制作了

我想在WPF和WindowsPhone8.1中开发一个应用程序。我想把一个变量的值从100增加到200,从200增加到300,然后继续。这是比赛的比分。现在我想在UI中设置动画。我已经编写了这样的示例代码

<Button Click="Button_Click" >Start</Button>
<TextBlock x:Name="tbvalue" />

但是使用这个我并没有实现反动画。任何关于如何实现此功能的想法或建议。

这就是我所做的,它运行良好。我制作了一个演示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>

ViewModel方法(当单击按钮时,这将使文本从0变为100)


你可以使用Dispatchermer及其开始和停止事件来做这件事负的是什么?它不起作用,它会增加值,但100200300不会像我在问题中提到的那样增加,但是仅供参考,我没有将其标记为反对票。哦,是的,它起作用,但非常缓慢,如何使它更快?如果我执行计数器_Timer.Interval=新的时间跨度(0,0,1,1,1000);它不工作,Dispatchermer_Tick(对象发送器,对象e)没有命中所有对象,我知道了,可以使用计数器_Timer.Interval=TimeSpan.frommissions(10)完成;这就是我要找的。谢谢,我没有提出任何不同的要求,在看到计数器的进度后,我注意到它非常慢,所以我认为毫秒可以完成这项工作。无论如何,谢谢你的帮助。我欠你的!
 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();
        }
        }
    <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>
public MainWindowViewModel()
{
   BtnClicked = new DelegateCommand(() => TextBlockValue += 100);
}

private double value;
public double TextBlockValue
{
    set
    {
        DispatcherTimer counterTimer = new DispatcherTimer
        {
            Interval = TimeSpan.FromMilliseconds(5),
        };
        counterTimer.Tick += (sender, args) =>
        {
            SetProperty(ref this.value, this.value + 1);
            if (this.value >= value) counterTimer.Stop();
        };
        counterTimer.Start();
    }
    get => value;
}