定时器/秒表用于自动化

时间:2011-05-31 03:00:09

标签: c# wpf

我有一个带有多边形针的圆形测量仪(进度条),当我在列表框中选择一个数字时,针上升到那个程度,类似于汽车的速度(工作正常)

我想要做的是自动运行列表项目的索引(0,1,2,3,...... 60),以便针头缓慢上升,直到达到60速度。然后,按住我想要的时间,我可以移动一个大头针并相应地运行里程表。我试图在MVC类中使用计时器并使用秒表。我可以选择索引0 -6,但它只会跳转到最后一个。我想尽力模拟汽车仪表板......你有什么想法?

public partial class MainWindow : Window
{
    //DispatcherTimer timer;
    List<Double> _items = new List<Double>();


    DispatcherTimer timer;

    public MainWindow()
    {
        this.InitializeComponent();
        listBox1.ItemsSource = new List<double>() { 0,1,2,3,4,5,10,11,12,13,14, 15, 20, 25, 30, 35, 40 };
        checkBox1.IsChecked = true;
        park.Foreground = Brushes.Red;

        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(2500);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();


    }

    void timer_Tick(object sender, EventArgs e)
    {



        int second = DateTime.Now.Second;

         firstDigitsec = second / 10;


        int secondDigitsec = second % 10;


        checkBox1.IsChecked = false;
        first.Foreground = Brushes.Green;
        park.Foreground = Brushes.White;
        checkBox2.IsChecked = true;



       Stopwatch stopwatch = Stopwatch.StartNew();

       listBox1.SelectedIndex = 0;
       listBox1.SelectedIndex = 1;
       listBox1.SelectedIndex = 2;
       listBox1.SelectedIndex = 3;


    }




    private int _stopw;
    public int sw
    {
        get { return _stopw; }
        set
        {
            _stopw = value;
            OnPropertyChanged("");

        }
    }



    private int _firstDigitsec;
    public int firstDigitsec
    {
        get { return _firstDigitsec; }
        set
        {
            _firstDigitsec = value;
             OnPropertyChanged("");
            /*
             if (firstDigitsec < 1)
             {
                 listBox1.SelectedIndex = 0;
             }
             if (firstDigitsec < 2)
             {

             */
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

1 个答案:

答案 0 :(得分:1)

由于您使用的是WPF,这听起来像是Animation

的绝佳候选人

使用Binding将Gauge的Progress属性绑定到viewmodel中的Progress属性,并应用DoubleAnimation将Progress属性从上一个值设置为新值