Пример #1
0
        public static void RegisterSystemEvent([NotNull] DependencyObject ownerWindow, [CanBeNull] SettingChangedEventHandler settingChanged = null)
        {
            Assert.ArgumentNotNull(ownerWindow, nameof(ownerWindow));

            if (settingChanged != null)
            {
                SettingChanged += settingChanged;
            }

            UnloadedEventHandler unloaded = null;

            unloaded = delegate(object sender, object o)
            {
                if (!ownerWindow.IsContainedIn(o))
                {
                    return;
                }

                if (settingChanged != null)
                {
                    SettingChanged -= settingChanged;
                }

                Unloaded -= unloaded;
            };

            Unloaded += unloaded;
        }
Пример #2
0
        /// <summary>
        ///     Register event handler for the chosen property and tag it.
        /// </summary>
        /// <typeparam name="TProperty">Type of the property</typeparam>
        /// <param name="handler">Handler to register</param>
        /// <param name="targetProperty">Lambda of style 'x => x.Property' or 'x => class.Property'</param>
        /// <param name="tag">Tag used for grouping</param>
        public void Subscribe <TProperty>(SettingChangedEventHandler <TProperty> handler,
                                          Expression <Func <TSettingClass, TProperty> > targetProperty, object tag)
        {
            var name = Extensions.GetPropertyName(targetProperty);

            _eventEntries.Add(new KeyValuePair <string, ISettingChangedHandlerEntry>(name,
                                                                                     new SettingChangedHandlerEntry <TProperty>(handler, tag)));
        }
        /// <summary>
        ///     Register event handler for the chosen property and tag it.
        /// </summary>
        /// <typeparam name="TProperty">Type of the property</typeparam>
        /// <param name="handler">Handler to register</param>
        /// <param name="selectedSetting">Lambda of style 'x => x.Property' or 'x => class.Property'</param>
        /// <param name="tag">Tag used for grouping</param>
        public void Subscribe <TProperty>(SettingChangedEventHandler <TProperty> handler,
                                          Expression <Func <TSettingClass, TProperty> > selectedSetting, object tag)
        {
            var name = ReflectionTools.GetPropertyName(selectedSetting);

            _eventEntries.Add(new KeyValuePair <string, ISettingChangedHandlerEntry>(name,
                                                                                     new SettingChangedHandlerEntry <TProperty>(handler, tag)));
        }
Пример #4
0
            internal SettingChangedHandlerEntry(SettingChangedEventHandler <T> handler, object tag)
            {
                if (handler == null)
                {
                    throw new ArgumentNullException(nameof(handler));
                }
                if (tag == null)
                {
                    throw new ArgumentNullException(nameof(tag));
                }

                Handler = handler;
                Tag     = tag;
            }
Пример #5
0
        /// <summary>
        ///     Manually bind to a setting
        /// </summary>
        /// <typeparam name="T">Bound value type</typeparam>
        /// <param name="setter">Delegate used to set value of the external property</param>
        /// <param name="getter">Delegate used to get value of the external property</param>
        /// <param name="registerEvent">Delegate used to register to the notifying event of the external property</param>
        /// <param name="unregisterEvent">Delegate used to unregister from the notifying event of the external property</param>
        /// <param name="targetSetting">Lambda of style 'x => x.Property' or 'x => class.Property'</param>
        /// <param name="tag">Tag used for grouping</param>
        public void Bind <T>(Action <T> setter, Func <T> getter,
                             Action <EventHandler> registerEvent, Action <EventHandler> unregisterEvent,
                             Expression <Func <TSettingClass, T> > targetSetting, object tag)
        {
            var memberSelectorExpression = targetSetting.Body as MemberExpression;

            if (memberSelectorExpression == null)
            {
                throw new ArgumentException("Invalid lambda format", nameof(targetSetting));
            }

            var property = memberSelectorExpression.Member as PropertyInfo;

            if (property == null)
            {
                throw new ArgumentException("Invalid lambda format", nameof(targetSetting));
            }

            EventHandler checkedChanged = (x, y) => { property.SetValue(Settings, getter(), null); };

            registerEvent(checkedChanged);

            SettingChangedEventHandler <T> settingChanged = (x, y) =>
            {
                var remoteValue = getter();
                if ((remoteValue != null && !remoteValue.Equals(y.NewValue)) ||
                    (remoteValue == null && y.NewValue != null))
                {
                    unregisterEvent(checkedChanged);
                    setter(y.NewValue);
                    registerEvent(checkedChanged);
                }
            };

            Subscribe(settingChanged, targetSetting, tag);
        }
 internal SettingChangedHandlerEntry(SettingChangedEventHandler <T> handler, object tag)
 {
     Handler = handler ?? throw new ArgumentNullException(nameof(handler));
     Tag     = tag ?? throw new ArgumentNullException(nameof(tag));
 }