public WeakCollectionViewWrapper(T innerCollection) { this.innerCollection = innerCollection; var currentChangedListener = new WeakEventListener <WeakCollectionViewWrapper <T>, object, EventArgs>(this) { OnEventAction = (instance, source, eventArgs) => instance.OnCurrentChanged(eventArgs), OnDetachAction = listener => innerCollection.CurrentChanged -= listener.OnEvent }; innerCollection.CurrentChanged += currentChangedListener.OnEvent; var currentChangingListener = new WeakEventListener <WeakCollectionViewWrapper <T>, object, CurrentChangingEventArgs>(this) { OnEventAction = (instance, source, eventArgs) => instance.OnCurrentChanging(eventArgs), OnDetachAction = listener => innerCollection.CurrentChanging -= listener.OnEvent }; innerCollection.CurrentChanging += currentChangingListener.OnEvent; var propertyChangedListener = new WeakEventListener <WeakCollectionViewWrapper <T>, object, NotifyCollectionChangedEventArgs>(this) { OnEventAction = (instance, source, eventArgs) => instance.OnCollectionChanged(eventArgs), OnDetachAction = listener => innerCollection.CollectionChanged -= listener.OnEvent }; innerCollection.CollectionChanged += propertyChangedListener.OnEvent; var enquireAboutItemVisibility = innerCollection as IEnquireAboutItemVisibility; if (enquireAboutItemVisibility != null) { var queryItemVisibilityListener = new WeakEventListener <WeakCollectionViewWrapper <T>, object, QueryItemVisibilityEventArgs>(this) { OnEventAction = (instance, source, eventArgs) => instance.OnQueryItemVisibility(eventArgs), OnDetachAction = listener => enquireAboutItemVisibility.QueryItemVisibility -= listener.OnEvent }; enquireAboutItemVisibility.QueryItemVisibility += queryItemVisibilityListener.OnEvent; } }
private static void AttachObservableModelCallback(DependencyObject source, DependencyPropertyChangedEventArgs args) { var typeName = args.NewValue as string; var view = source as FrameworkElement; if (typeName == null || view == null) { return; } var modelType = Type.GetType("Raven.Studio.Models." + typeName) ?? Type.GetType(typeName); if (modelType == null) { return; } try { var model = (Model)Activator.CreateInstance(modelType); model.ForceTimerTicked(); var observable = (IObservable)Activator.CreateInstance(typeof(Observable <>).MakeGenericType(modelType)); observable.Value = model; view.DataContext = observable; SetPageTitle(model, view); var weakListener = new WeakEventListener <IObservable, object, RoutedEventArgs>(observable); view.Loaded += weakListener.OnEvent; weakListener.OnEventAction = OnViewLoaded; weakListener.OnDetachAction = listener => view.Loaded -= listener.OnEvent; } catch (Exception ex) { throw new InvalidOperationException(string.Format("Cannot create instance of model type: {0}", modelType), ex); } }