Silverlight MVVM
来源:互联网 发布:乐高机器人编程视频 编辑:程序博客网 时间:2024/04/29 21:07
通知属性修改
using System;using System.ComponentModel;using System.Linq.Expressions;using System.Runtime.Serialization;namespace Microsoft.Practices.Prism.ViewModel{ [DataContract] public abstract class NotificationObject : INotifyPropertyChanged { protected NotificationObject(); public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression); protected void RaisePropertyChanged(params string[] propertyNames); protected virtual void RaisePropertyChanged(string propertyName); }}
Command 注册类
using System;namespace Microsoft.Practices.Prism.Commands{ public class DelegateCommand<T> : DelegateCommandBase { public DelegateCommand(Action<T> executeMethod); public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod); public bool CanExecute(T parameter); public void Execute(T parameter); }}
添加触发事件
添加一个触发事件类
using System;using System.Net;using System.Reflection;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Interactivity;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace DTO.SL{ /// <summary> /// Trigger触发的事件类 /// </summary> public class ExecuteCommandAction : TriggerAction<FrameworkElement> { public static readonly DependencyProperty CommandNameProperty = DependencyProperty.Register("CommandName", typeof(string), typeof(ExecuteCommandAction), null); public string CommandName { get { return (string)GetValue(CommandNameProperty); } set { SetValue(CommandNameProperty, value); } } public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommandAction), null); public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } private static bool IsCommandProperty(PropertyInfo property) { return typeof(ICommand).IsAssignableFrom(property.PropertyType); } /// <summary> /// 重写Invoke方法 /// </summary> /// <param name="parameter"></param> protected override void Invoke(object parameter) { if (AssociatedObject == null) { return; } ICommand command = null; var dataContext = AssociatedObject.DataContext; foreach (var item in dataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (IsCommandProperty(item) && string.Equals(item.Name, CommandName, StringComparison.Ordinal)) { command = (ICommand)item.GetValue(dataContext, null); } } if ((command != null) && command.CanExecute(CommandParameter)) { command.Execute(CommandParameter); } } }}
Xaml代码
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:myConvert="clr-namespace:DTO.SL;assembly=DTO.SL" <Button Width="100" Height="50" Content="大" > <Custom:Interaction.Triggers> <Custom:EventTrigger EventName="Click"> <myConvert:ExecuteCommandAction CommandName="MaxCommand"></myConvert:ExecuteCommandAction> </Custom:EventTrigger> </Custom:Interaction.Triggers> </Button>
CS代码
using Microsoft.Practices.Prism.ViewModel;using Microsoft.Practices.Prism.Commands; private ICommand maxCommand; public ICommand MaxCommand { get { return maxCommand; } set { maxCommand = value; } } MaxCommand = new DelegateCommand(() => { //事件内容 });
0 0
- Silverlight/MVVM
- Silverlight MVVM
- MVVM for silverlight
- silverlight mvvm sample
- Silverlight的MVVM模式
- Silverlight MVVM 触发器
- silverlight wcf mvvm
- Silverlight + Model-View-ViewModel (MVVM)
- Silverlight + Model-View-ViewModel (MVVM)
- Silverlight中使用MVVM(1)
- Silverlight中使用MVVM(2)
- Silverlight中使用MVVM(3)
- Silverlight中使用MVVM(4)
- Silverlight MVVM模式开发 -MVVMLight
- SilverLight中MVVM设计模式
- wpf silverlight mvvm mvc 种种
- MVVM Silverlight 5 Datagrid MVVM 鼠标双击事件
- [Silverlight入门系列]使用MVVM模式(1):MVVM核心概念
- Spring事务传播属性和隔离级别
- CentOs添加开机重启服务
- 黑客高手是这样上网的!Vimium、Vimperator 浏览器插件让你全程用键盘快捷高效浏览网页
- 时尚的左侧菜单
- ldap的启动问题
- Silverlight MVVM
- Scripts:查询sga中各组件使用率的脚本perf_sga_usage.sql
- 用NSURLConnection Post上传数据的"+"会变成空格
- 在Xcode中使用Git进行源码版本控制
- 如何保护Excel工作表,不被人修改或删除指定区域
- 高斯函数
- 未来三天华北黄淮有高温天气 河北山东局地达40℃
- 精通安卓性能优化-第二章(一)
- 学魅族手滑,中兴红牛V5抢购标错价