2

我想在 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();
}  

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

4

2 回答 2

3

这就是我所做的并且工作正常。我制作了一个演示 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>
于 2014-11-12T12:35:40.960 回答
0

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

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;
}
于 2021-01-18T08:42:49.690 回答