【问题标题】:Move Slider until Value is reached移动滑块直到达到值
【发布时间】:2014-06-25 23:32:44
【问题描述】:

我尝试在某个值为 true 时“动画化”滑块,直到它达到指定值。但由于某种原因,它并没有我想象的那么容易:

while (depthScan == true)
{
    depth_max_slider.Value += 10;
    Console.WriteLine("Working");
    if (blobCount <= 400)
    {
        depth_max_slider.Value += 10;

    }
    else
    {
        depthScan = false;
    }

}

我做错了什么?

【问题讨论】:

  • 到底出了什么问题?请提供有关它在做什么的信息。
  • 基本上整个程序冻结并在后台运行脚本(这是逻辑,因为它是一个while循环),用户不会注意到。我希望滑块具有动画效果,以便用户可以看到正在发生的事情。
  • blobCount 是否已更新?似乎你的 else 永远不会触发,所以你的循环永远不会结束。
  • 此外,这里似乎没有任何东西会限制循环以使幻灯片对用户可见。这可能会非常快地循环,使您的滑块很快超过您的预期最大值。
  • 是的,正在更新 blobCount。是的,该程序运行速度很快,但即使我将计数设置为 4000 或将增量设置为非常慢的数字(如 0.1),该程序仍将运行直到完成 - 不显示任何动画。我读过关于线程的文章,这个概念在这里有用吗?

标签: c# wpf while-loop slider


【解决方案1】:

这样的事情应该可以解决问题......

public class MyWindow : Window
    {
        void StartButton_Click( object sender, RoutedEventArgs e )
        {
            // This method will run on the UI thread

            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += worker_DoWork;
            worker.WorkerReportsProgress = true;
            worker.ProgressChanged += worker_ProgressChanged;

            worker.RunWorkerAsync(); // start the thread

            // UI thread continues immediately

            // exit click event handler, UI thread goes back to WPF control so it can keep rendering...
        }

        void worker_ProgressChanged( object sender, ProgressChangedEventArgs e )
        {
            // This method will run on the UI thread

            double depth_max = (double) e.UserState; // grab the value of depth_max passed over from background thread

            depth_max_slider.Value = depth_max; // update the slider with this value

            // exit ProgressChanged event handler, UI thread goes back to WPF control so it can keep rendering...
        }

        void worker_DoWork( object sender, DoWorkEventArgs e )
        {
            // This method will run on the background thread

            BackgroundWorker worker = sender as BackgroundWorker;

            int percentComplete = 0;
            double depth_max;

            while( depthScan == true )
            {
                depth_max += 10; // NOTE: don't touch the slider control from the background thread, use a double instead!

                worker.ReportProgress( percentComplete, depth_max ); // pass depth_max as the 2nd param, "user state", will show up in ProgressChangedEventArgs in ProgressChanged handler

                Console.WriteLine( "Working" );
                if( blobCount <= 400 )
                {
                    depth_max += 10;

                    worker.ReportProgress( percentComplete, depth_max );

                }
                else
                {
                    depthScan = false;
                }

            }
        }
    }

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 2016-08-15
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多