Silverlight实战示例6(兼集合属性的妙用)--客户端视图模型层(VM)

Silverlight实战示例6(兼集合属性的妙用)--客户端视图模型层(VM),第1张

概述3)视图模型层DynamicDataViewModel .cs using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Inpu

3)视图模型层Dynamicdataviewmodel .cs

using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using MAppStructure.Datasource;
using System.ComponentModel;
using System.Collections.Generic;
using MEntitIEs;
namespace MAppStructure.viewmodel
{
    /// <summary>
    /// 视图模型层,注意必须实现INotifyPropertyChanged接口,这样VM层才能绑定。
    /// viewmodel可以做一个基类,这个基类主要是实现INotifyPropertyChanged接口
    /// 和你需要的一些公共方法,这里为了简单些,就没有做基类了.
    /// </summary>
    public class Dynamicdataviewmodel : INotifyPropertyChanged
    {
        /// <summary>
        /// 视图模型层主要采用的模型层服务实例,注意
        /// 可以调用多个模型层得服务实例,这里是演示,所以只做了一个。
        /// </summary>
        private DynamicdataSource theS;
        public Dynamicdataviewmodel()
        {
            theS = new DynamicdataSource();
            //初始化并注册命令.
            Commands = new Dictionary<string,MyCommand>();
            Commands.Add("button1Command",new MyCommand(OnbuttonCommand));
        }
        private voID OnbuttonCommand(object parameter)
        {
            LoadData();
            MessageBox.Show("ok");
        }
       
        /// <summary>
        ///命令字典集合,这样做的好处一是可以减少定义命令的硬代码,同时提供了一种动
        ///态命令的可能性,并有利于扩展.
        /// </summary>
        public Dictionary<string,MyCommand> Commands
        {
            get;
            private set;
        }
        private List<DynamicdaTarow> _DataSource;
        public List<DynamicdaTarow> DataSource
        {
            get
            {
                return _DataSource;
            }
            private set
            {
                _DataSource = value;
                RaisePropertyChanged("DataSource");
              
            }
        }
        private Dynamicdatatable _Datatable;
        /// <summary>
        /// 获取来的动态数据表.
        /// </summary>
        public Dynamicdatatable Datatable
        {
            get
            {
                return _Datatable;
            }
            private set
            {
                _Datatable = value;
                RaisePropertyChanged("Datatable");

            }
        }
        /// <summary>
        /// 数据加载.
        /// </summary>
        private voID LoadData()
        {
            theS.GetDynamicdatatable("select * from EmployeeInfo ",op =>
                {
                    if (op.HasError == false)
                    {
                        DataSource = op.Value.Rows;
                        Datatable = op.Value;
                    }
                    else
                    {
                        MessageBox.Show(op.ErrorMsg);
                    }
                },null);
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected voID RaisePropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this,new PropertyChangedEventArgs(propertyname));
            }
        }
    }
    /// <summary>
    /// 自己的命令类,主要为了命令的绑定.这里是典型的命令模式,命令的接收者是本VM.不过这里的
    /// 命令接收者并没有作为命令的成员,而是采用委托方式,在这种情况下更为便利。
    /// </summary>
    public class MyCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }
        private Action<object> _action;
        public MyCommand(Action<object> Action)
        {
            this._action = Action;
        }
        public event EventHandler CanExecuteChanged;

        public voID Execute(object parameter)
        {
            if (_action != null)
            {
                _action(parameter);
            }
        }
    }
}
这里大家注意的是,我的命令采用的不是一般的定义方式,而是采用字典集合的方式进行,好处上面讲了,请大家注意页面的绑定方式。

 


原文链接: http://www.voidcn.com/article/p-nqqbbsrs-bcq.html 总结

以上是内存溢出为你收集整理的Silverlight实战示例6(兼集合属性的妙用)--客户端视图模型层(VM)全部内容,希望文章能够帮你解决Silverlight实战示例6(兼集合属性的妙用)--客户端视图模型层(VM)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1069364.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存