Пример #1
0
 public SubscriptionToken SubscribePropertyChanged(
     IPropertyChangedPubSubViewModel source,
     string propertyName,
     bool keepSubscriberReferenceAlive)
 {
     return(SubscribePropertyChanged(source, propertyName, propertyName, keepSubscriberReferenceAlive));
 }
Пример #2
0
 public SubscriptionToken SubscribePropertyChanged(
     IPropertyChangedPubSubViewModel source,
     string sourcePropertyName,
     string targetPropertyName)
 {
     return(SubscribePropertyChanged(source, sourcePropertyName, targetPropertyName, ThreadOption.PublisherThread));
 }
Пример #3
0
 public SubscriptionToken SubscribePropertyChanged(
     IPropertyChangedPubSubViewModel source,
     string propertyName,
     ThreadOption threadOption)
 {
     return(SubscribePropertyChanged(source, propertyName, propertyName, threadOption));
 }
Пример #4
0
 public SubscriptionToken SubscribePropertyChanged(
     IPropertyChangedPubSubViewModel source,
     string sourcePropertyName,
     string targetPropertyName,
     bool keepSubscriberReferenceAlive)
 {
     return(SubscribePropertyChanged(source, sourcePropertyName, targetPropertyName, ThreadOption.PublisherThread, keepSubscriberReferenceAlive));
 }
Пример #5
0
        public SubscriptionToken SubscribePropertyChanged(
            IPropertyChangedPubSubViewModel source,
            string sourcePropertyName,
            string targetPropertyName,
            ThreadOption threadOption,
            bool keepSubscriberReferenceAlive)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (string.IsNullOrWhiteSpace(sourcePropertyName))
            {
                throw new ArgumentNullException(nameof(sourcePropertyName));
            }
            if (!source.ContainsReadableProperty(sourcePropertyName))
            {
                throw new InvalidOperationException($"{sourcePropertyName} is not a public, readable instance property on {source.GetType().FullName} (instance ID: {source.InstanceId})");
            }
            if (string.IsNullOrWhiteSpace(targetPropertyName))
            {
                throw new ArgumentNullException(nameof(targetPropertyName));
            }
            if (!ContainsReadableProperty(targetPropertyName))
            {
                throw new InvalidOperationException($"{targetPropertyName} is not a public, readable instance property on {GetType().FullName} (instance ID: {InstanceId})");
            }

            Dictionary <string, HashSet <string> > sourceSubscribedProperties = m_SourceSubscribedPropertyNames.GetOrCreateValue(source);

            if (!sourceSubscribedProperties.TryGetValue(sourcePropertyName, out HashSet <string> subscribedPropertyTargets))
            {
                subscribedPropertyTargets = new HashSet <string>();
                sourceSubscribedProperties.Add(sourcePropertyName, subscribedPropertyTargets);
            }

            if (subscribedPropertyTargets.Contains(targetPropertyName))
            {
                throw new InvalidOperationException($"{GetType().FullName} (instance ID: {InstanceId}) {targetPropertyName} property is already subscribed to {source.GetType().FullName} (instance {source.InstanceId}) {sourcePropertyName} property");
            }

            // Need to create the delegates this way in order for the event aggregator to retain the weak reference.
            var action = (Action <PropertyChangedPubSubPayload>)GetType()
                         .GetRuntimeMethods()
                         .First(x => string.CompareOrdinal(x.Name, nameof(SubscriptionAction)) == 0)
                         .CreateDelegate(typeof(Action <PropertyChangedPubSubPayload>), this);

            var filter = (Predicate <PropertyChangedPubSubPayload>)GetType()
                         .GetRuntimeMethods()
                         .First(x => string.CompareOrdinal(x.Name, nameof(SubscriptionFilter)) == 0)
                         .CreateDelegate(typeof(Predicate <PropertyChangedPubSubPayload>), this);

            subscribedPropertyTargets.Add(targetPropertyName);

            return(m_EventService.GetEvent <PubSubEvent <PropertyChangedPubSubPayload> >()
                   .Subscribe(action, threadOption, keepSubscriberReferenceAlive, filter));
        }