2016-09-14 7 views
-3

wpfとMVVM構造を使って抽選番号生成プログラムを書いてみたい。私は以下のコードを書いたが、何も働かなかった。誰もが私を助けることができますか? 私は間違いを発見してビルドしてデバッグしました!抽選番号生成プログラム

あなたMainViewModelで
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

()この行を追加します。編集

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分後

、私は...

& &にPropertyChangedをあなたが欠けている2つの言葉を考え出し=でヌル:!

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

テキストブロック行を ''に変更します。テキストをコードから変更して表示するには、2つのモードが必要です。 –

+0

その時点でyuと同意します。しかし、それはまだ動作していません。 –

+0

最後の愚かな提案: 'Binding Path = Text'のような' Path = ... 'を' Binding Text'にすべて削除し、ボタンと同じです。 –

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(); 
    } 
}