2016-09-14 20 views
-3

我想用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(); 
    } 
} 
+4

什麼是不wroking? – fubo

+0

你在構造函數中準備了定時器,但是你永遠不會調用'StartTimer()'方法... –

+0

我在按鈕中調用。所以當我點擊它會啓動timer.but但它並沒有啓動 –

回答

0

在你MainViewModel()加入這一行:編輯

public MainViewModel() 
{ 
    this.Duration = 1000; 
    this.Text = "00"; 
    timer = new DispatcherTimer(); 
    timer.Interval = this.Duration; 
    timer.Tick += new EventHandler(TimerTick); 
    this.StartTimerCommand = new Delegatecommon(this.StartTimer); 
    this.StopTimerCommand = new Delegatecommon(this.StopTimer); 
} 

public void StartTimer() 
{ 
    timer.Start(); 
} 

public void StopTimer() 
{ 
    timer.Stop(); 
} 

保持休息和以前一樣。

審查的30分鐘後,我想通了,2個字,你就錯過......

& &的PropertyChanged = NULL在:

class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertychange(string propertyname) 
    { 
     if (propertyname != null && PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
     } 
    } 
} 

添加的是,它工作時,我測試你的代碼,我要求你在這裏做額外的修改。

+0

根本不工作。 –

+0

嘗試編輯的代碼。基本上,你初始化定時器,並在開始時設置所有的東西,留下2個按鈕來簡單地切換'timer.Start()'和'timer.Stop()' –

+0

謝謝。我已經嘗試過了,但仍然沒有任何工作。 –

0
<Window x:Class="Randm.View.MainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:Randm.ViewModel" 
    mc:Ignorable="d" 
    Title="RandomNumber" Height="300" Width="300"> 
<Window.DataContext> 
    <local:MainViewModel/> 
</Window.DataContext> 

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition></ColumnDefinition> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
    </Grid.RowDefinitions> 
    <TextBlock FontSize="50" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Path=Text,UpdateSourceTrigger=PropertyChanged}"></TextBlock> 
    <StackPanel Grid.Row="1"> 
    <Button Width="100" Height="50" Content="Start" FontSize="20" Command="{Binding Path=StartTimerCommand}"></Button> 
    <Button Width="100" Height="50" Content="Stop" FontSize="20" Command="{Binding Path=StopTimerCommand}"></Button> 
    </StackPanel> 
</Grid> 

class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertychange(string propertyname) 
    { 
     if(propertyname == null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
     } 
    } 
} 
+0

將文本塊行更改爲''。您需要雙向模式才能從代碼更改文本並顯示它。 –

+0

同意yu在那一點。但是,它仍然不起作用。 –

+0

最後一個愚蠢的建議:刪除所有'路徑= ...',如'綁定路徑=文本'到'綁定文本'和按鈕相同。 –

0
class Delegatecommon : ICommand 
{ 
    private Action _execute; 
    private Func<bool> _canexecute; 

    public Delegatecommon(Action ac) : this(ac,() => true) { } 

    public Delegatecommon(Action ac, Func<bool> fu) 
    { 
     if (ac == null) 
      throw new ArgumentNullException(); 
     else if (fu == null) 
      throw new ArgumentNullException(); 
     _execute = ac; 
     _canexecute = fu; 
    } 

    event EventHandler ICommand.CanExecuteChanged 
    { 
     add 
     { 
      CommandManager.RequerySuggested += value; 
     } 

     remove 
     { 
      CommandManager.RequerySuggested -= value; 
     } 
    } 

    public void execute() 
    { 
     _execute(); 
    } 

    public bool canexecute() 
    { 
     return _canexecute(); 
    } 
    bool ICommand.CanExecute(object parameter) 
    { 
     return canexecute(); 
    } 

    void ICommand.Execute(object parameter) 
    { 
     execute(); 
    } 
}