我想用wpf和MVVM結構編寫一個開獎號碼生成程序。我寫了下面的代碼,但沒有任何工作。你能幫助我嗎? 我發現沒有錯誤,並建立和調試!開獎號碼生成程序
class MainViewModel : ViewModelBase
{
private int duration;
private string text;
private DispatcherTimer timer = null;
public MainViewModel()
{
this.Duration = 1000;
this.Text = "00";
this.StartTimerCommand = new Delegatecommon(this.StartTimer);
this.StopTimerCommand = new Delegatecommon(this.StopTimer);
}
#region Properties
public int Duration
{
get
{
return this.duration;
}
set
{
this.duration = value;
RaisePropertychange("Duration");
}
}
public string Text
{
get
{
return this.text;
}
set
{
this.text = value;
RaisePropertychange("Text");
}
}
public Delegatecommon StartTimerCommand
{
get;
set;
}
public Delegatecommon StopTimerCommand
{
get;
set;
}
#endregion
public void StartTimer()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(this.Duration);
timer.Tick += new EventHandler(TimerTick);
timer.Start();
}
public void StopTimer()
{
if (timer != null)
{
timer.Stop();
timer = null;
}
}
private void TimerTick(object send, EventArgs e)
{
Random rnd = new Random((Int32)DateTime.Now.Ticks);
this.Text = rnd.Next(0, 100).ToString();
}
}
什麼是不wroking? – fubo
你在構造函數中準備了定時器,但是你永遠不會調用'StartTimer()'方法... –
我在按鈕中調用。所以當我點擊它會啓動timer.but但它並沒有啓動 –