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

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - 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结构的大概介绍