private void AssignAction(T instance, SubmitAction action) { EntitySession.TrackedItem item; this.tracked.TryGetValue(instance, out item); switch (action) { case SubmitAction.None: case SubmitAction.Update: case SubmitAction.Insert: case SubmitAction.InsertOrUpdate: case SubmitAction.Delete: this.tracked[instance] = new EntitySession.TrackedItem(this, instance, (item != null) ? item.Original : null, action, (item != null) ? item.HookedEvent : false); break; case SubmitAction.PossibleUpdate: { INotifyPropertyChanging changing = instance as INotifyPropertyChanging; if (changing == null) { object original = this.Mapping.CloneEntity(base.Entity, instance); this.tracked[instance] = new EntitySession.TrackedItem(this, instance, original, SubmitAction.PossibleUpdate, false); break; } if (!item.HookedEvent) { changing.PropertyChanging += new PropertyChangingEventHandler(this.OnPropertyChanging); } this.tracked[instance] = new EntitySession.TrackedItem(this, instance, null, SubmitAction.PossibleUpdate, true); break; } default: throw new InvalidOperationException(string.Format("Unknown SubmitAction: {0}", action)); } }
public void Add(TValue item) { if (_primaryIndex == null) //it does not have a primary index { _naturalIndex.Add(item); } else //it has a primary index { object key = _propertyGetterCallback(item, null); _primaryIndex.Add(key, item); } if (_secondaryIndices != null) { foreach (XbimSecondaryIndex <TValue> secondary in _secondaryIndices) { secondary.Add(item); } } INotifyPropertyChanging iNotChanging = item as INotifyPropertyChanging; if (iNotChanging != null) { iNotChanging.PropertyChanging += XbimIndexedCollection_PropertyChanging; } INotifyPropertyChanged iNotChanged = item as INotifyPropertyChanged; if (iNotChanged != null) { iNotChanged.PropertyChanged += XbimIndexedCollection_PropertyChanged; } }
protected override void ClearItems() { lock (_lockObj) { this.CheckReentrancy(); int iCnt = base.Items.Count; for (int i = 0; i < iCnt; i++) { if (i >= base.Items.Count) { break; } T item = base.Items[i]; INotifyPropertyChanged npc = item as INotifyPropertyChanged; if (npc != null) { npc.PropertyChanged -= new PropertyChangedEventHandler(Item_PropertyChanged); } INotifyPropertyChanging npc2 = item as INotifyPropertyChanging; if (npc2 != null) { npc2.PropertyChanging -= new PropertyChangingEventHandler(Item_PropertyChanging); } } base.ClearItems(); this.OnPropertyChanged(CountString); this.OnPropertyChanged(IndexerName); this.OnCollectionReset(); HookItemsCleared(); } }
protected override void InsertItem(int index, T item) { lock (_lockObj) { if (base.Contains(item)) { return; } INotifyPropertyChanged npc = item as INotifyPropertyChanged; if (npc != null) { npc.PropertyChanged += new PropertyChangedEventHandler(Item_PropertyChanged); } INotifyPropertyChanging npc2 = item as INotifyPropertyChanging; if (npc2 != null) { npc2.PropertyChanging += new PropertyChangingEventHandler(Item_PropertyChanging); } this.CheckReentrancy(); base.InsertItem(index, item); this.OnPropertyChanged(CountString); this.OnPropertyChanged(IndexerName); //this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index); this.OnCollectionReset(); this.ActiveItem = item; HookItemAdded(item); } }
public static IObservable <PropertyChangingEventArgs> ListenPropertyChanging( this INotifyPropertyChanging listenee) { return(Observable.FromEventPattern <PropertyChangingEventHandler, PropertyChangingEventArgs> (h => listenee.PropertyChanging += h, h => listenee.PropertyChanging -= h) .Select(p => p.EventArgs)); }
/// <summary>Remove <paramref name="handler"/> to <see cref="INotifyPropertyChanging.PropertyChanging"/> with null check.</summary> public static void Remove(this INotifyPropertyChanging sender, PropertyChangingEventHandler handler) { if (sender is null) { return; } sender.PropertyChanging -= handler; }
private void Detach(object obj) { INotifyPropertyChanging notifier = obj as INotifyPropertyChanging; if (notifier != null) { notifier.PropertyChanging -= this.onPropertyChanging; } }
public static IDisposable ListenPropertyChanging( this INotifyPropertyChanging source, Action <PropertyChangingEventArgs> handler) { var h = new PropertyChangingEventHandler((_, e) => handler(e)); source.PropertyChanging += h; return(Disposable.Create(() => source.PropertyChanging -= h)); }
public void RegisterNotifyingObject(INotifyPropertyChanging obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj), "Contract assertion not met: obj != null"); } obj.PropertyChanging += (sender, e) => ProcessNotification(sender, e); }
/// <summary> /// 속성 설정 /// </summary> /// <typeparam name="TProperty">속성 형식</typeparam> /// <param name="obj">속성을 포함한 객체</param> /// <param name="target">속성 저장 멤버</param> /// <param name="value">설정 값</param> /// <param name="propertyChangingEvent">PropertyChanging 이벤트</param> /// <param name="propertyName">속성 명</param> /// <returns>값 변경 여부</returns> public static bool Set <TProperty>(this INotifyPropertyChanging obj, ref TProperty target, TProperty value, PropertyChangingEventHandler propertyChangingEvent, [CallerMemberName] string propertyName = null) { if (!EqualityComparer <TProperty> .Default.Equals(target, value)) { propertyChangingEvent?.Invoke(obj, new PropertyChangingEventArgs(propertyName)); target = value; return(true); } return(false); }
public static IObservable <PropertyChangingEventArgs> ListenPropertyChanging <T>( this INotifyPropertyChanging listenee, Expression <Func <T> > propertyExpression) { String propname = ExtractPropertyName(propertyExpression); if (propname == null) { throw new ArgumentException("Unknown property name."); } return(ListenPropertyChanging(listenee) .Where(p => p.PropertyName == propname)); }
public static void RemoveHandler(INotifyPropertyChanging source, EventHandler <PropertyChangingEventArgs> handler) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } CurrentManager.ProtectedRemoveHandler(source, handler); }
private void UnsubscribeIfNoMoreListeners() { if (!this.HasActiveListeners) { WeakPropertyChangedEventManager.UnregisterSource(_source); _source.PropertyChanged -= OnPropertyChanged; INotifyPropertyChanging sourceAsINotifyPropertyChanging = _source as INotifyPropertyChanging; if (sourceAsINotifyPropertyChanging != null) { sourceAsINotifyPropertyChanging.PropertyChanging -= OnPropertyChanging; } } }
/// <summary> /// 속성 설정 /// </summary> /// <typeparam name="TProperty">속성 형식</typeparam> /// <param name="obj">속성을 포함한 객체</param> /// <param name="target">속성 저장 멤버</param> /// <param name="value">설정 값</param> /// <param name="propertyChangingEvent">PropertyChanging 이벤트</param> /// <param name="propertyName">속성 명</param> /// <returns>값 변경 여부</returns> public static bool Set <TProperty>(this INotifyPropertyChanging obj, ref TProperty target, TProperty value, PropertyChangingEventHandler propertyChangingEvent, [CallerMemberName] string propertyName = null) { if (!EqualityComparer <TProperty> .Default.Equals(target, value)) { var eventArgs = new QueryPropertyChangingEventArgs <TProperty>(propertyName, value); propertyChangingEvent?.Invoke(obj, eventArgs); if (!eventArgs.IsCanceled) { target = value; return(true); } } return(false); }
public WeakPropertyBridge(INotifyPropertyChanged source) { _source = source; _propertyNameToCallbacks = new HybridDictionary(); source.PropertyChanged += OnPropertyChanged; INotifyPropertyChanging sourceAsINotifyPropertyChanging = source as INotifyPropertyChanging; if (sourceAsINotifyPropertyChanging != null) { sourceAsINotifyPropertyChanging.PropertyChanging += OnPropertyChanging; } }
public static void PropertyChangingRemoveHandler(this INotifyPropertyChanging source, EventHandler <PropertyChangingEventArgs> handler) { if (source == null) { ThrowException.ThrowArgumentNullException("source"); } if (handler == null) { ThrowException.ThrowArgumentNullException("handler"); } WeakEventManager <INotifyPropertyChanging, PropertyChangingEventArgs> .RemoveHandler(source, "PropertyChanging", handler); }
private void Attach(object obj) { INotifyPropertyChanging notifier = obj as INotifyPropertyChanging; if (notifier != null) { notifier.PropertyChanging += this.onPropertyChanging; } else { // if has no notifier, consider it modified already this.OnPropertyChanging(obj, null); } }
public static IObservable <T> Observe <T>(this INotifyPropertyChanging source, string propertyName) where T : INotifyPropertyChanging { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } return(Observable.FromEventPattern <PropertyChangingEventHandler, PropertyChangingEventArgs>(h => source.PropertyChanging += h, h => source.PropertyChanging -= h) .Where(_ => _.EventArgs.PropertyName == propertyName) .Select(_ => (T)_.Sender)); }
public static void Raise(this PropertyChangingEventHandler handler, INotifyPropertyChanging source, [CallerMemberName] string propertyName = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } var args = new PropertyChangingEventArgs(propertyName); handler?.Invoke(source, args); }
public static void PropertyChangingEvent(INotifyPropertyChanging inpc, Action action, Action <PropertyChangingEventArgs> eventAction, int fireCount = 1) { EventCore( inpc, action, eventAction, (o, handler) => { PropertyChangingEventHandler x = (sender, args) => handler(sender, args); inpc.PropertyChanging += x; return(x); }, (o, handler) => inpc.PropertyChanging -= handler, fireCount ); }
public static IDisposable ListenPropertyChanging <T>( this INotifyPropertyChanging source, Expression <Func <T> > propertyExpression, Action <PropertyChangingEventArgs> handler) { var propName = ExtractPropertyName(propertyExpression); if (propName == null) { throw new ArgumentException("Unknown property name."); } return(source.ListenPropertyChanging(e => { if (e.PropertyName == propName) { handler(e); } })); }
private bool processing; // True we're processing a change notification. // Used to avoid infinite recursion. /// <summary> /// Constructor. /// </summary> /// <param name="entity">The entity instance whose properties are being mapped.</param> /// <exception cref="ArgumentException"> /// Thrown if the entity doesn't implement either of <b>INotifyPropertyChanged</b> /// or <b>INotifyPropertyChanging</b>. /// </exception> public PropertyChangeMap(TEntity entity) { INotifyPropertyChanged changedInstance = entity as INotifyPropertyChanged; #if !SILVERLIGHT INotifyPropertyChanging changingInstance = entity as INotifyPropertyChanging; #endif if (entity == null) { throw new ArgumentNullException("entity"); } if (changedInstance == null #if !SILVERLIGHT && changingInstance == null #endif ) { throw new ArgumentException("Entity must implement one of INotifyPropertyChanged or INotifyPropertyChanging."); } this.processing = false; this.associations = null; this.entity = entity; if (changedInstance != null) { changedInstance.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged); } #if !SILVERLIGHT if (changingInstance != null) { changingInstance.PropertyChanging += new PropertyChangingEventHandler(OnPropertyChanging); } #endif }
public void NotifyPropertyChanging() { Item item = new Item() { Name = "abc" }.AsTrackable(); INotifyPropertyChanging proxy = (INotifyPropertyChanging)item; bool raised = false; proxy.PropertyChanging += (obj, args) => { if (item.Name == "abc") { raised = true; } }; item.Name = "123"; Assert.True(raised); }
private void AssignAction(T instance, SubmitAction action) { TrackedItem ti; this.tracked.TryGetValue(instance, out ti); switch (action) { case SubmitAction.Insert: case SubmitAction.InsertOrUpdate: case SubmitAction.Update: case SubmitAction.Delete: case SubmitAction.None: this.tracked[instance] = new TrackedItem(this, instance, ti != null ? ti.Original : null, action, ti != null ? ti.HookedEvent : false); break; case SubmitAction.PossibleUpdate: INotifyPropertyChanging notify = instance as INotifyPropertyChanging; if (notify != null) { if (!ti.HookedEvent) { notify.PropertyChanging += new PropertyChangingEventHandler(this.OnPropertyChanging); } this.tracked[instance] = new TrackedItem(this, instance, null, SubmitAction.PossibleUpdate, true); } else { var original = this.Mapping.CloneEntity(this.Entity, instance); this.tracked[instance] = new TrackedItem(this, instance, original, SubmitAction.PossibleUpdate, false); } break; default: throw new InvalidOperationException(string.Format("Unknown SubmitAction: {0}", action)); } }
/// <summary> /// Raises an event safely, ensuring that all handlers are called on the proper thread, and any exceptions do not prevent other handlers being called. /// </summary> /// <param name="handler">The event to raise.</param> /// <param name="sender">The sender of the event.</param> /// <param name="propertyName">The name of the property relating to this event.</param> /// <exception cref="ArgumentNullException">Thrown if propertyName is null or an empty string.</exception> /// <exception cref="AggregateException">Thrown if any handlers raise exceptions, with the exceptions raised captured in the <see cref="AggregateException.InnerExceptions"/> property.</exception> public static void SafeRaise(this PropertyChangingEventHandler handler, INotifyPropertyChanging sender, string propertyName) { propertyName.ThrowIfNullOrEmpty(nameof(propertyName)); sender?.GetType().ValidatePropertyName(propertyName, nameof(propertyName)); if (handler is null) { return; } PropertyChangingEventArgs e = new (propertyName); List <Exception> raisedExceptions = null; foreach (Delegate del in handler.GetInvocationList()) { try { del.DynamicInvoke(sender, e); } catch (TargetInvocationException ex) when(ex.InnerException is Exception) { if (raisedExceptions is null) { raisedExceptions = new List <Exception>(); } raisedExceptions.Add(ex.InnerException); } } // Check list of exceptions is either still null, or not empty. Debug.Assert(raisedExceptions is null || raisedExceptions.Any(), "Empty list of exceptions after handling event."); if (raisedExceptions is List <Exception> ) { throw new AggregateException(Properties.Resources.SafeRaiseExceptionMessage, raisedExceptions); } }
protected override void StopListening(object source) { INotifyPropertyChanging typedSource = (INotifyPropertyChanging)source; typedSource.PropertyChanging -= new PropertyChangingEventHandler(OnPropertyChanging); }
protected override void RemoveEventHandler(INotifyPropertyChanging source) { source.PropertyChanging -= OnEvent; }
public WeakNotifyPropertyChangingHandler(INotifyPropertyChanging source, TSubscriber subscriber, [EmptyCapture] Action <TSubscriber, object, PropertyChangingEventArgs> weakHandler) : base(source, subscriber, weakHandler) { source.PropertyChanging += OnEvent; }
public static IDisposable Observing(this INotifyPropertyChanging notify, PropertyChangingEventHandler handler) { notify.PropertyChanging += handler; return(new DisposableAction(() => notify.PropertyChanging -= handler)); }
/// <summary> /// Registers a weak handler to <see cref="INotifyPropertyChanging.PropertyChanging"/>. /// </summary> /// <typeparam name="TSubscriber">The type of the event subscriber.</typeparam> /// <param name="source">The event source.</param> /// <param name="subscriber">The event subscriber.</param> /// <param name="weakHandler">The weak handler.</param> /// <returns>A registration object that can be used to deregister from the event.</returns> public static IDisposable RegisterPropertyChangingWeak <TSubscriber>(this INotifyPropertyChanging source, TSubscriber subscriber, [EmptyCapture] Action <TSubscriber, object, PropertyChangingEventArgs> weakHandler) where TSubscriber : class { return(new WeakNotifyPropertyChangingHandler <TSubscriber>(source, subscriber, weakHandler)); }
public void SubscribeToPropertyChangingEvents(INotifyPropertyChanging obj) { Assert.IsFalse(SubscribedPropertyChangingObjects.Contains(obj)); obj.PropertyChanging += OnPropertyChanging; SubscribedPropertyChangingObjects.Add(obj); }
public void RegisterNotifyingObject(INotifyPropertyChanging obj) { Contract.Requires<ArgumentNullException>(obj != null); obj.PropertyChanging += (sender, e) => ProcessNotification(sender, e); }