【问题标题】:c# - 通过字符串列表“循环动画”
【发布时间】:2020-05-04 09:37:09
【问题描述】:

我正在构建一个非常简单的 C# WPF 应用程序。该应用程序的作用是,当我按下按钮时,它会从字符串列表中随机显示一个值。

我想要实现的是一个动画,它可以快速滚动列表中的所有值(显示在标签中),然后开始减速并停止在列表中的随机字符串上。几乎就像一个“旋转/滚动/闪烁的文本轮”。

我对编程还是很陌生(C# 学习的第二天),所以如果有人能指出我正确的方向,我会很高兴。用定时器循环?

List<string> randomStrings = new List<string>();

        public MainWindow()
        {
            InitializeComponent();
            randomString.Add("Abcd");
            randomString.Add("Water");
            randomString.Add("Moon");
            randomString.Add("Pizza");
            randomString.Add("Winter");
            randomString.Add("Orange");

            MyRandomStrings.ItemsSource = randomString; //Showing which strings in box.
        }

        public void GetRandomString()
        {
            Random r = new Random();
            int index = r.Next(randomString.Count);
            string myRandomString = randomString[index]; //Fetch a random string
        
            Result.Content = myRandomString; //Sets the label. I want this to be "animated".
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            GetRandomString();
        }

【问题讨论】:

    标签: c# wpf animation


    【解决方案1】:

    您可以使用Task.Delay 来创建延迟并仍然创建顺序代码而不是状态机或计时器等。

    这是一个例子:

    它在 WindowsForm 上使用标签和按钮。因此,只需稍作调整,您就可以使其适用于您的配置。

    public partial class MainWindow : Form
    {
        private List<string> _randomStrings = new List<string>();
        private Random _rnd = new Random(DateTime.UtcNow.Millisecond);
    
        public MainWindow()
        {
            InitializeComponent();
            _randomStrings.Add("Abcd");
            _randomStrings.Add("Water");
            _randomStrings.Add("Moon");
            _randomStrings.Add("Pizza");
            _randomStrings.Add("Winter");
            _randomStrings.Add("Orange");
        }
    
        private async void button1_Click(object sender, EventArgs e)
        {
            // determine the rollcount. (some initial rolls for the rolling effect)
            int rollCount = 50 + _rnd.Next(_randomStrings.Count);
    
            int index = 0;
    
            // roll it.......
            for (int i = 0; i < rollCount; i++)
            {
                // just use a modulo on the i to get an index which is inside the list.
                index = i % _randomStrings.Count;
    
                // display the current item
                label1.Text = _randomStrings[index];
    
                // calculate a delay which gets longer and longer each roll.
                var delay = (250 * i / rollCount);
    
                // wait some. (but don't block the UI)
                await Task.Delay(delay);
            }
    
            MessageBox.Show($"and.... The winner is... {_randomStrings[index]}");
        }
    }
    

    【讨论】:

    • 它完全符合我的要求。我尝试了秒表,但它阻止/挂起整个 UI。 Task.Delay 对我来说是新事物。谢谢杰伦。
    • 这就是异步/等待的力量。编写顺序代码,将其作为状态机运行。 ;-) 别忘了阻止第二次执行(因为你可以多次按下按钮,执行会混淆。)
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 2013-11-23
    • 2017-03-21
    • 1970-01-01
    • 2020-06-21
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多