문제

I want to random a number of my array. Then I show it in a textblock. I want to do it every second. How to update my textblock every second ?. Please to help me.

도움이 되었습니까?

해결책

Create a dispatcher timer and for every ticks update your textbock.

number = 0
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    //if you use binding and mvvm
    this.Text = number.tostring();
    //if you don't use binding
    yourTextblock.Text = number.toString();
    number ++;
 }

다른 팁

create dispatcher timer and every half second update text box

    int number=0;
    private DispatcherTimer _timer;
    public sample()
    {
        InitializeComponent();
        _timer = new DispatcherTimer();
        _timer.Interval = new TimeSpan(0, 0, 0,0,500);
        _timer.Tick += _timer_Tick;
        _timer.Start();
    }
    void _timer_Tick(object sender, EventArgs e)
    {
      number++;
      yourTextblock.Text = number.toString();
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top