• Caliburn笔记Dependent Action(wpf框架)


       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);
        }
    }
  • 相关阅读:
    Mysql索引管理与优化
    用git报错,解决Unable to negotiate with **** port 22: no matching host key type found. Their offer: sshrsa
    SQL中limit的用法
    将 RealProxy 使用迁移到 DispatchProxy
    学习记录:定义动态程序集和动态模块
    python 自定义的异常类
    python 进程池
    python selenium自动化测试模块
    SQL DDL DML DQL DCL
    mysql 索引
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1636244.html
Copyright © 2020-2023  润新知