SilverLight框架初探-ViewModel

上一篇介绍了MVVM架构的总体架构,下面介绍一下里面ViewModel与RiaService的实现过程

1.ViewModel,定义与View相对应的属性与操作,如下:

ViewModelBase.cs,这里继承自INotifyPropertyChanged,这样,当与之所绑定的View发生变化时,就能触发ViewModel相对应的属性或方法。代码如下:

 

    public class ViewModelBase:INotifyPropertyChanged
    {

        protected void OnNotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }
     
        public bool IsDesignTime
        {
            get
            {
                return (Application.Current == null) || (Application.Current.GetType() == typeof(Application));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

 

ProductionViewModel.cs,与View相对应的ViewModel,继承自ViewModelBase,代码如下:

public class ProductionViewModel:ViewModelBase
    {
        private IEnumerable<ProductionDataDto> _listProduction;    //查询结果
        private string _searchText;               //查询条件
        private ICommand _queryCommand;          //查询命令

        public ICommand QueryCommand
        {
            get { return _queryCommand; }
        }
        public string SearchText
        {

            get { return _searchText; }
            set
            {
                _searchText = value;
                OnNotifyPropertyChanged("SearchText");
            }
        }

        public IEnumerable<ProductionDataDto> ListProduction
        {
            get { return _listProduction; }
            set
            {
                _listProduction = value;
                OnNotifyPropertyChanged("ListProduction");
            }
        }
       
        TestDomainContext service = null;
        public ProductionViewModel()
        {
            _queryCommand = new QueryCommand(this);
            _queryCommand.Execute(null);
        }


        public void QueryData()
        {
                service = new YOCTestDomainContext();

                _listProduction = service.Load(service.GetProductionDataQuery()).Entities;
            
        }
    }

 

这里,我们定义了一个ICommand命令,主要用于处理ViewModel中涉及到行为,代码如下:

 public partial class QueryCommand:ICommand
    {
        private ProductionViewModel _productionViewModel;
        private string _searchText;
        public QueryCommand(ProductionViewModel productionViewModel,string searchText)
        {
            _productionViewModel = productionViewModel;
            _searchText = searchText;
        }
        public QueryCommand(ProductionViewModel productionViewModel)
        {
            _productionViewModel = productionViewModel;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { }
            remove { }
        }
        public void Execute(object parameter)
        {
            this._productionViewModel.QueryData();
        }
    }

 

上面就是对于ViewModel结构的大概介绍

 

你可能感兴趣的:(silverlight)