惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
Y
Y Combinator Blog
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
博客园_首页
大猫的无限游戏
大猫的无限游戏
美团技术团队
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
小众软件
小众软件
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 【当耐特】
J
Java Code Geeks
F
Fortinet All Blogs
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美

博客园 - yo

Silverlight 缺陷 - yo - 博客园 SilverLight Controls WF 4.0 中 Persistence 异常 - yo SilverLight框架初探-View - yo - 博客园 SilverLight框架初探-RiaService SilverLight框架初探 与客户“调情” AG_E_PARSER_BAD_PROPERTY_VALUE 定义属于自己的Routing 数据契约的序列化 WCF客户端配置问题 关闭EXCEL进程 母版页中控件ID获取 - yo - 博客园 Reportviewer - Error: ASP.NET session has expired - yo SQL CLR C# DLL动态调用 Sharepoint List faults - yo 出错页面webpar的t删除 quickpart
SilverLight框架初探-ViewModel
yo · 2010-03-15 · via 博客园 - yo

上一篇介绍了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结构的大概介绍