彩票号码发生器程序

时间:2016-09-14 08:20:26

标签: c# wpf mvvm

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

3 个答案:

答案 0 :(得分: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 in:

class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

补充一点,它在我测试你的代码时有用,我要求你在这里进行额外的修改。

答案 1 :(得分: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));
        }
    }
}

答案 2 :(得分: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();
    }
}