WPF比较两个随机数大小写,利用MVVM思想实现

MVVM模式是把表现层和业务层完全分离,所以这里就使用MVVM制作一个极其简单的WPF的例子:

先看看最终图:

上图,需要实现的是,界面两个按钮,一个是生成随机两个数,一个是对两个数比较,把大的数显示出来。所以需要三个属性,两个事件。

由于逻辑比较简单,不用写model等类,实现如下:

1、创建基类NotifictionObject.cs,实现接口INotifyPropertyChanged

 class NotifictionObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string str)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(str));
        }

    }

2、创建DelegateCommand.cs,继承ICommand

  class DelegateCommand : ICommand
    {
        public Func<object,bool> CanExecuteFunc { get; set; }

        public Action<object> ExecuteAction { get; set; }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc == null)
                return true;
            return CanExecuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if (ExecuteAction == null)
                return;
            ExecuteAction(parameter);
        }
    }

3、再创建类MainWindowViewModel.cs,继承NotifictionObject基类

 

class MainWindowViewModel : NotifictionObject
    {
        private int a;

        public int RndA
        {
            get { return a; }
            set
            {
                a = value;
                RaisePropertyChanged("RndA");
            }
        }
        private int b;

        public int RndB
        {
            get { return b; }
            set
            {
                b = value;
                RaisePropertyChanged("RndB");
            }
        }
        private int result;

        public int Result
        {
            get { return result; }
            set
            {
                result = value;
                RaisePropertyChanged("Result");
            }
        }

        public DelegateCommand CompareCommand { get; set; }
        public DelegateCommand ReNewRndNumCommand { get; set; }

        Random rnd = new Random();
        public MainWindowViewModel()
        {
            LoadData();

            CompareCommand = new DelegateCommand();
            CompareCommand.ExecuteAction = (para) =>
            {
                Result = RndA >= RndB ? RndA : RndB;
            };

            ReNewRndNumCommand = new DelegateCommand();
            ReNewRndNumCommand.ExecuteAction = (para) =>
            {
                LoadData();
            };
        }

        public void LoadData()
        {
            RndA = rnd.Next(0, 100);
            RndB = rnd.Next(0, 100);
        }
    }

 

4、类都实现了,现在实现界面部分

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Margin="10"  x:Name="textBlock" TextWrapping="Wrap" Text="数字1:"/>
        <TextBlock Margin="10" x:Name="textBlock1" Grid.Column="1" TextWrapping="Wrap" Text="{Binding RndA}"/>
        <TextBlock Margin="10" x:Name="textBlock2" Grid.Column="2" TextWrapping="Wrap" Text="数字2:"/>
        <TextBlock Margin="10"  x:Name="textBlock3" Grid.Column="3" TextWrapping="Wrap" Text="{Binding RndB}"/>

        <Button Grid.Row="1" Command="{Binding ReNewRndNumCommand}" Grid.ColumnSpan="2" Width="75" Margin="10">重新生成数</Button>
        <Button x:Name="button" Command="{Binding CompareCommand}" Margin="10" Content="比较大小" Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="2" Width="75"/>
       
        <TextBlock x:Name="textBlock4" Margin="10" HorizontalAlignment="Center" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="4" TextWrapping="Wrap" Text="{Binding Result}"/>

    </Grid>

5.在主界面的C#初始化时添加上下文:

public MainWindow()
{
   InitializeComponent();

   this.DataContext = new MainWindowViewModel();
}

代码就不详细介绍了,自己慢慢看,不是太难,多多练习。

 

posted @ 2015-09-12 15:27  lunawzh  阅读(372)  评论(0编辑  收藏  举报