Caliburn笔记-Dependent Action(wpf框架)
2009-12-30 17:45 Clingingboy 阅读(755) 评论(0) 编辑 收藏 举报http://caliburn.codeplex.com/wikipage?title=Dependent%20Actions&referringTitle=Documentation
这一部分功能也属于过滤器.再来看两个接口
FilterManager会在构造函数中,对实现该接口的Filter进行初始化
public interface IInitializable : IFilter { /// <summary> /// Initializes the filter. /// </summary> /// <param name="targetType">Type of the target.</param> /// <param name="metadataContainer">The metadata container.</param> /// <param name="serviceLocator">The serviceLocator.</param> void Initialize(Type targetType, IMetadataContainer metadataContainer, IServiceLocator serviceLocator); }
public FilterManager(Type targetType, IMetadataContainer metadataContainer, IServiceLocator serviceLocator) { … filters.OfType<IInitializable>() .Apply(x => x.Initialize(targetType, metadataContainer, serviceLocator)); …
}
IHandlerAware用来实现对事件的监听
public interface IHandlerAware : IFilter { /// <summary> /// Makes the filter aware of the <see cref="IRoutedMessageHandler"/>. /// </summary> /// <param name="messageHandler">The message handler.</param> void MakeAwareOf(IRoutedMessageHandler messageHandler); /// <summary> /// Makes the filter aware of the <see cref="IMessageTrigger"/>. /// </summary> /// <param name="messageHandler">The message handler.</param> /// <param name="trigger">The trigger.</param> void MakeAwareOf(IRoutedMessageHandler messageHandler, IMessageTrigger trigger); }
如DependenciesAttribute的实现,DependencyObserver对实现INotifyPropertyChanged的对象进行了监听.
public void MakeAwareOf(IRoutedMessageHandler messageHandler) { var notifier = messageHandler.Unwrap() as INotifyPropertyChanged; if (notifier == null) return; var helper = messageHandler.GetMetadata<DependencyObserver>(); if (helper != null) return; helper = new DependencyObserver(messageHandler, notifier); messageHandler.AddMetadata(helper); }
然后在wpf元素初始化后,再次调用,添加事件
public void MakeAwareOf(IRoutedMessageHandler messageHandler, IMessageTrigger trigger) { var helper = messageHandler.GetMetadata<DependencyObserver>(); if (helper == null) return; if(trigger.Message.RelatesTo(_target)) helper.MakeAwareOf(trigger, _dependencies); }
DependencyObserver的MakeAwareOf.本质上海还是添加了MessageTrigger
public void MakeAwareOf(IMessageTrigger trigger, IEnumerable<string> dependencies) { foreach (var dependency in dependencies) { IList<IMessageTrigger> triggers; if(!_triggersToNotify.TryGetValue(dependency, out triggers)) { triggers = new List<IMessageTrigger>(); _triggersToNotify[dependency] = triggers; } if(!triggers.Contains(trigger)) triggers.Add(trigger); } }