コード例 #1
0
        public static void SubscribeToPropertyChange <T>(this T objectThatNotifies, Expression <Func <T, object> > property,
                                                         PropertyChangedHandler <T> handler) where T : INotifyPropertyChanged
        {
            PropertyInfo info = objectThatNotifies.GetPropertyInfo(property);

            subscribe(objectThatNotifies, info, handler);
        }
コード例 #2
0
        /// <summary>
        /// Subscribe to changes in an object implementing INotifiyPropertyChanged.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objectThatNotifies">The object you are interested in.</param>
        /// <param name="property">The property you are interested in.</param>
        /// <param name="handler">The delegate that will handle the event.</param>
        public static void SubscribeToChange <T>(this T objectThatNotifies, Expression <Func <object> > property,
                                                 PropertyChangedHandler <T> handler) where T : INotifyPropertyChanged
        {
            // Add a new PropertyChangedEventHandler
            objectThatNotifies.PropertyChanged += (s, e) =>
            {
                // Get name of Property
                var lambda = property as LambdaExpression;
                MemberExpression memberExpression;
                if (lambda.Body is UnaryExpression)
                {
                    var unaryExpression = lambda.Body as UnaryExpression;
                    memberExpression = unaryExpression.Operand as MemberExpression;
                }
                else
                {
                    memberExpression = lambda.Body as MemberExpression;
                }
                var propertyInfo = memberExpression.Member as PropertyInfo;

                // Notify handler if PropertyName is the one we were interested in
                if (e.PropertyName.Equals(propertyInfo.Name))
                {
                    handler(objectThatNotifies);
                }
            };
        }
コード例 #3
0
        protected void BuildCategoriesTree(ItemsList contents, TreeItem parentItem, ICollection <string> categories, ICollection <string> allSelectedCategories,
                                           IDictionary <string, ICollection <string> > categories2Children, IDictionary <string, MediaCategory> allCategories)
        {
            contents.Clear();
            List <string> categoriesSorted = new List <string>(categories);

            categoriesSorted.Sort();
            foreach (string mediaCategory in categoriesSorted)
            {
                TreeItem categoryItem = new TreeItem(Consts.KEY_NAME, mediaCategory);
                categoryItem.AdditionalProperties[Consts.KEY_MEDIA_CATEGORY] = allCategories[mediaCategory];
                categoryItem.AdditionalProperties[Consts.KEY_PARENT_ITEM]    = parentItem;
                if (allSelectedCategories.Contains(mediaCategory))
                {
                    categoryItem.Selected = true;
                }
                PropertyChangedHandler handler = (property, oldValue) => OnMediaCategoryItemSelectionChanged(categoryItem);
                categoryItem.SelectedProperty.Attach(handler);
                categoryItem.AdditionalProperties["HandlerStrongReference"] = handler; // SelectedProperty only manages weak references to its handlers -> avoid handler to be removed
                ICollection <string> childCategories;
                if (!categories2Children.TryGetValue(mediaCategory, out childCategories))
                {
                    childCategories = new List <string>();
                }
                BuildCategoriesTree(categoryItem.SubItems, categoryItem, childCategories, allSelectedCategories, categories2Children, allCategories);
                contents.Add(categoryItem);
            }
        }
コード例 #4
0
 public Listener()
 {
     handler = new PropertyChangedHandler(toBeListenedObject)
     {
         { "PropertyA", DoA },
         { "PropertyB", DoB }
     };
 }
コード例 #5
0
        private static void AddHandler(INotifyPropertyChanged dataContext,
                                       TextBlock textBlock)
        {
            if (dataContext == null) return;

            var propertyChangedHandler = new PropertyChangedHandler(textBlock);
            dataContext.PropertyChanged += propertyChangedHandler.PropertyChanged;
            Handlers[dataContext] = propertyChangedHandler;
        }
コード例 #6
0
ファイル: SProperty.cs プロジェクト: git-thinh/MediaPortal-2
        public override void Fire(object oldValue)
        {
            PropertyChangedHandler dlgt = _eventDelegate;

            if (dlgt != null)
            {
                dlgt(this, oldValue);
            }
        }
コード例 #7
0
        protected void OnPropertyChanged(String propName, Object oldValue, Object newValue)
        {
            PropertyChangedHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedHandlerEventArgs(propName, oldValue, newValue));
            }
        }
コード例 #8
0
 private static void subscribe <T>(T objectThatNotifies, PropertyInfo info, PropertyChangedHandler <T> Handler) where T : INotifyPropertyChanged
 {
     objectThatNotifies.PropertyChanged += (s, e) =>
     {
         if (e.PropertyName.Equals(info.Name))
         {
             Handler(objectThatNotifies);
         }
     };
 }
コード例 #9
0
ファイル: BaseEntity.cs プロジェクト: githuer/DMSFrame
 internal void SetPropertyChanged(bool bPropertyChanged)
 {
     if (bPropertyChanged)
     {
         _OnPropertyChanged += OnEntityPropertyChanged;
     }
     else
     {
         _OnPropertyChanged -= OnEntityPropertyChanged;
     }
 }
コード例 #10
0
ファイル: BaseEntity.cs プロジェクト: githuer/DMSFrame
 internal void SetMappingPropertyChanged(bool bMappingPropertyChanged)
 {
     if (bMappingPropertyChanged)
     {
         _OnMappingPropertyChanged += OnEntityMappingPropertyChanged;
     }
     else
     {
         _OnMappingPropertyChanged -= OnEntityMappingPropertyChanged;
     }
 }
コード例 #11
0
 /// <summary>
 /// Creates am instance of the class
 /// </summary>
 /// <param name="defaultValue">Default for non existing key-value pairs</param>
 internal PropertyBagCollection(TValue defaultValue, PropertyChangedHandler propertyChangedHandler = null, KeyValuePair <string, TValue>[] initialValues = null)
 {
     DefaultValue           = defaultValue;
     PropertyChangedHandler = propertyChangedHandler;
     if (null != initialValues)
     {
         foreach (KeyValuePair <string, TValue> item in initialValues)
         {
             this[item.Key] = item.Value;
         }
     }
 }
コード例 #12
0
ファイル: DockObject.cs プロジェクト: slackstone/tuxjunior
        protected void EmitPropertyEvent(string name)
        {
            // Make a local assignment of the handler here to prevent
            // any race conditions if the PropertyChanged value changes
            // to null after the != null check.
            PropertyChangedHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, name);
            }
        }
コード例 #13
0
        public int MatchingBackingFieldAndPropertyReturnType(object initialValue, object value)
        {
            //Arrange.
            NpcClass npc = new NpcClass(initialValue);
            PropertyChangedHandler handler = new PropertyChangedHandler(npc);

            //Act.
            npc.Data = value;

            //Assert.
            return(handler.EventCount);
        }
コード例 #14
0
        public int SaveNonNullableInNullable(DateTime?initialValue, DateTime value)
        {
            //Arrange.
            NullableNpcClass       npc     = new NullableNpcClass(initialValue);
            PropertyChangedHandler handler = new PropertyChangedHandler(npc);

            //Act.
            npc.Data = value;

            //Assert.
            return(handler.EventCount);
        }
コード例 #15
0
ファイル: BaseEntity.cs プロジェクト: githuer/DMSFrame
 /// <summary>
 /// BaseEntity 构造方法
 /// </summary>
 /// <param name="bPropertyChanged">是否记录值改变事件</param>
 /// <param name="bMappingPropertyChanged">是否记录MAPPING改变信息</param>
 internal BaseEntity(bool bPropertyChanged, bool bMappingPropertyChanged)
 {
     _ChangedMappingProperties = new Dictionary <string, object>();
     _ChangedProperties        = new Dictionary <string, object>();
     if (bPropertyChanged)
     {
         _OnPropertyChanged += OnEntityPropertyChanged;
     }
     if (bMappingPropertyChanged)
     {
         _OnMappingPropertyChanged += OnEntityMappingPropertyChanged;
     }
 }
コード例 #16
0
ファイル: BindingPath.cs プロジェクト: propellingbits/Uno
            /// <summary>
            /// Subscribes to property notifications for the current binding
            /// </summary>
            /// <param name="action">The action to execute when new values are raised</param>
            /// <returns>A disposable to be called when the subscription is disposed.</returns>
            private IDisposable SubscribeToPropertyChanged(PropertyChangedHandler action)
            {
                var disposable = new CompositeDisposable();

                foreach (var handler in _propertyChangedHandlers)
                {
                    object previousValue = null;

                    Action updateProperty = () =>
                    {
                        var newValue = GetSourceValue();

                        action(previousValue, newValue, shouldRaiseValueChanged: true);

                        previousValue = newValue;
                    };

                    Action disposeAction = () =>
                    {
                        action(previousValue, DependencyProperty.UnsetValue, shouldRaiseValueChanged: false);
                    };

                    var handlerDisposable = handler(_dataContextWeakStorage, PropertyName, updateProperty);

                    if (handlerDisposable != null)
                    {
                        previousValue = GetSourceValue();

                        // We need to keep the reference to the updatePropertyHandler
                        // in this disposable. The reference is attached to the source's
                        // object lifetime, to the target (bound) object.
                        //
                        // The registerations made by _propertyChangedHandlers are all
                        // weak with regards to the delegates that are provided.
                        disposable.Add(() => updateProperty = null);

                        disposable.Add(handlerDisposable);
                        disposable.Add(disposeAction);
                    }
                }

                return(disposable);
            }
コード例 #17
0
        /// <summary>
        /// Subscribe to changes in an object implementing INotifiyPropertyChanged.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ObjectThatNotifies">The object you are interested in.</param>
        /// <param name="Property">The property you are interested in.</param>
        /// <param name="Handler">The delegate that will handle the event.</param>
        public static PropertyChangedEventHandler SubscribeToChange <T> (this T ObjectThatNotifies, Expression <Func <object> > Property, PropertyChangedHandler <T> Handler)
            where T : INotifyPropertyChanged
        {
            string propertyName             = GetPropertyName(Property);
            PropertyChangedEventHandler evh = (s, e) => {
                if (e.PropertyName.Equals(propertyName))
                {
                    Handler(ObjectThatNotifies);
                }
            };

            ObjectThatNotifies.PropertyChanged += evh;
            return(evh);
        }
コード例 #18
0
 /// <summary>
 /// Detaches the specified event handler.
 /// </summary>
 /// <param name="handler">The handler.</param>
 public abstract void Detach(PropertyChangedHandler handler);
コード例 #19
0
ファイル: WProperty.cs プロジェクト: pacificIT/MediaPortal-2
 /// <summary>
 /// Attaches an event handler. The event handler will be bound with a weak reference.
 /// </summary>
 /// <param name="handler">The handler.</param>
 public override void Attach(PropertyChangedHandler handler)
 {
     _eventDelegate.Attach(handler);
 }
コード例 #20
0
 /// <summary>
 /// Detaches the specified event handler.
 /// </summary>
 /// <param name="handler">The handler.</param>
 public abstract void Detach(PropertyChangedHandler handler);
コード例 #21
0
ファイル: WProperty.cs プロジェクト: HAF-Blade/MediaPortal-2
 /// <summary>
 /// Attaches an event handler. The event handler will be bound with a weak reference.
 /// </summary>
 /// <param name="handler">The handler.</param>
 public override void Attach(PropertyChangedHandler handler)
 {
   _eventDelegate.Attach(handler);
 }
コード例 #22
0
        public static void AddChangeHandler <T>(this T element, DependencyProperty dependencyProperty, PropertyChangedHandler <T> handler)
            where T : FrameworkElement
        {
            var key = element;
            IDependencyPropertySubscription subscription = null;

            if (ExpressionSubscriptions.ContainsKey(key))
            {
                subscription = ExpressionSubscriptions[key];
            }
            else
            {
                subscription = new DependencyPropertySubscription <T>((T)element, dependencyProperty);
                ExpressionSubscriptions[key] = subscription;
            }

            ((INotifyPropertyChanged)subscription).SubscribeToChange(() => subscription.Value, o => handler((T)element));
        }
コード例 #23
0
        public static void AddChangeHandler <T>(this T element, Expression <Func <object> > expression, PropertyChangedHandler <T> handler)
            where T : FrameworkElement
        {
            string propertyPath;
            var    propertyInfo       = expression.GetPropertyInfo();
            var    dependencyProperty = DependencyPropertyHelper.GetDependencyPropertyByPropertyName(element, propertyInfo.Name, out propertyPath);

            AddChangeHandler(element, dependencyProperty, handler);
        }
コード例 #24
0
        private void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
            case nameof(PrefixListText):
                PrefixList = PrefixListText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                break;

            case nameof(UserBlacklistText):
                UserBlacklist = UserBlacklistText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                break;

            case nameof(UserNicknamesText):
                UserNicknames =
                    new Dictionary <string, string>(
                        UserNicknamesText
                        .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(x => x.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries))
                        .ToDictionary(x => x[0], y => y[1]),
                        StringComparer.OrdinalIgnoreCase);
                break;

            // For every class we have to call this pattern
            case nameof(SubscriberNotification):
                SubscriberNotification.PropertyChanged += (s, e) => Safe(this);
                SubscriberNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(RaidNotification):
                RaidNotification.PropertyChanged += (s, e) => Safe(this);
                RaidNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(UserJoinedNotification):
                UserJoinedNotification.PropertyChanged += (s, e) => Safe(this);
                UserJoinedNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(UserLeftNotification):
                UserLeftNotification.PropertyChanged += (s, e) => Safe(this);
                UserLeftNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(BeingHostedNotification):
                BeingHostedNotification.PropertyChanged += (s, e) => Safe(this);
                BeingHostedNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(MessageNotification):
                MessageNotification.PropertyChanged += (s, e) => Safe(this);
                MessageNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(ClientConnectedNotification):
                ClientConnectedNotification.PropertyChanged += (s, e) => Safe(this);
                ClientConnectedNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(NewFollowerNotification):
                NewFollowerNotification.PropertyChanged += (s, e) => Safe(this);
                NewFollowerNotification.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;

            case nameof(BabelSettings):
                BabelSettings.PropertyChanged += (s, e) => Safe(this);
                BabelSettings.PropertyChanged += (s, e) => PropertyChangedHandler.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Settings)));
                break;
            }

            Safe(this);
        }
コード例 #25
0
ファイル: ViewModel.cs プロジェクト: time4vps/vpn-win-app
 public void OnPropertyChanged([CallerMemberName] string name = null)
 {
     PropertyChangedHandler?.Invoke(this, new PropertyChangedEventArgs(name));
 }
コード例 #26
0
ファイル: SProperty.cs プロジェクト: git-thinh/MediaPortal-2
 /// <summary>
 /// Detaches the specified event handler.
 /// </summary>
 /// <param name="handler">The handler.</param>
 public override void Detach(PropertyChangedHandler handler)
 {
     _eventDelegate -= handler;
 }
コード例 #27
0
 protected void OnPropertyChanged(string propertyName)
 {
     PropertyChangedHandler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 }
コード例 #28
0
        public static void SubscribeToChange <T>(this T objectThatNotifies, Expression <Func <object> > expression, PropertyChangedHandler <T> handler)
            where T : INotifyPropertyChanged
        {
            objectThatNotifies.PropertyChanged +=
                (s, e) =>
            {
                var lambda = expression as LambdaExpression;
                MemberExpression memberExpression;
                if (lambda.Body is UnaryExpression)
                {
                    var unaryExpression = lambda.Body as UnaryExpression;
                    memberExpression = unaryExpression.Operand as MemberExpression;
                }
                else
                {
                    memberExpression = lambda.Body as MemberExpression;
                }
                var propertyInfo = memberExpression.Member as PropertyInfo;

                if (e.PropertyName.Equals(propertyInfo.Name))
                {
                    handler(objectThatNotifies);
                }
            };
        }
コード例 #29
0
        public static void SubscribeToChange <T>(this T objectThatNotifies, Expression <Func <object> > expression, PropertyChangedHandler <T> handler)
            where T : INotifyPropertyChanged
        {
            var thread = Thread.CurrentThread;

            objectThatNotifies.PropertyChanged +=
                (s, e) =>
            {
                var memberExpression = expression.GetMemberExpression();
                var propertyInfo     = memberExpression.Member as PropertyInfo;

                if (e.PropertyName.Equals(propertyInfo.Name))
                {
                    if (Deployment.Current.Dispatcher.CheckAccess())
                    {
                        handler(objectThatNotifies);
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(handler, objectThatNotifies);
                    }
                }
            };
        }
コード例 #30
0
 public static void SubscribeToChange(this INotifyPropertyChanged objectThatNotifies, Expression <Func <object> > expression, PropertyChangedHandler <INotifyPropertyChanged> handler)
 {
     SubscribeToChange <INotifyPropertyChanged>(objectThatNotifies, expression, handler);
 }
コード例 #31
0
ファイル: SProperty.cs プロジェクト: chekiI/MediaPortal-2
 /// <summary>
 /// Detaches the specified event handler.
 /// </summary>
 /// <param name="handler">The handler.</param>
 public override void Detach(PropertyChangedHandler handler)
 {
   _eventDelegate -= handler;
 }
コード例 #32
0
ファイル: ViewModel.cs プロジェクト: time4vps/vpn-win-app
 public void OnPropertyChangedAsync(string name)
 {
     PropertyChangedHandler.BeginInvoke(this, new PropertyChangedEventArgs(name), null, null);
 }
コード例 #33
0
 public static void SubscribeToChange(this INotifyPropertyChanged objectThatNotifies, string propertyName, PropertyChangedHandler <INotifyPropertyChanged> handler)
 {
     objectThatNotifies.PropertyChanged +=
         (s, e) =>
     {
         if (e.PropertyName.Equals(propertyName))
         {
             handler(objectThatNotifies);
         }
     };
 }
コード例 #34
0
 /// <summary>Casts the given object to an 'INotifyPropertyChanged' and if it is observable un-wires the given event handler from the 'PropertyChanged' event.</summary>
 /// <param name="model">The model to un-wire.</param>
 /// <param name="handler">The handler to remove.</param>
 public static void UnlistenPropertyChanged(object model, PropertyChangedHandler handler)
 {
     INotifyPropertyChanged observable = model as INotifyPropertyChanged;
     if (observable != null) observable.PropertyChanged -= handler;
 }