/// <summary> /// Initializes a new instance of the <see cref="LogicBase"/> class. /// </summary> /// <param name="targetView">The target control.</param> /// <param name="viewModelType">Type of the view model.</param> /// <param name="viewModel">The view model.</param> /// <exception cref="ArgumentNullException">The <paramref name="targetView"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="viewModelType"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="viewModelType"/> does not implement interface <see cref="IViewModel"/>.</exception> protected LogicBase(IView targetView, Type viewModelType = null, IViewModel viewModel = null) { if (CatelEnvironment.IsInDesignMode) { return; } Argument.IsNotNull("targetView", targetView); var targetViewType = targetView.GetType(); if (viewModelType is null) { viewModelType = (viewModel != null) ? viewModel.GetType() : _viewModelLocator.ResolveViewModel(targetViewType); if (viewModelType is null) { throw Log.ErrorAndCreateException <NotSupportedException>($"The view model of the view '{targetViewType.Name}' could not be resolved. Make sure to customize the IViewModelLocator or register the view and view model manually"); } } UniqueIdentifier = UniqueIdentifierHelper.GetUniqueIdentifier <LogicBase>(); Log.Debug($"Constructing behavior '{GetType().Name}' for '{targetView.GetType().Name}' with unique id '{UniqueIdentifier}'"); TargetView = targetView; ViewModelType = viewModelType; ViewModel = viewModel; ViewModelBehavior = (viewModel != null) ? LogicViewModelBehavior.Injected : LogicViewModelBehavior.Dynamic; if (ViewModel != null) { SetDataContext(ViewModel); } Log.Debug("Subscribing to view events"); ViewLoadManager.AddView(this); if (this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, nameof(IViewLoadManager.ViewLoading), OnViewLoadedManagerLoadingInternal, false) is null) { Log.Debug("Failed to use weak events to subscribe to 'ViewLoadManager.ViewLoading', going to subscribe without weak events"); ViewLoadManager.ViewLoading += OnViewLoadedManagerLoadingInternal; } if (this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, nameof(IViewLoadManager.ViewLoaded), OnViewLoadedManagerLoadedInternal, false) is null) { Log.Debug("Failed to use weak events to subscribe to 'ViewLoadManager.ViewLoaded', going to subscribe without weak events"); ViewLoadManager.ViewLoaded += OnViewLoadedManagerLoadedInternal; } if (this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, nameof(IViewLoadManager.ViewUnloading), OnViewLoadedManagerUnloadingInternal, false) is null) { Log.Debug("Failed to use weak events to subscribe to 'ViewLoadManager.ViewUnloading', going to subscribe without weak events"); ViewLoadManager.ViewUnloading += OnViewLoadedManagerUnloadingInternal; } if (this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, nameof(IViewLoadManager.ViewUnloaded), OnViewLoadedManagerUnloadedInternal, false) is null) { Log.Debug("Failed to use weak events to subscribe to 'ViewLoadManager.ViewUnloaded', going to subscribe without weak events"); ViewLoadManager.ViewUnloaded += OnViewLoadedManagerUnloadedInternal; } // Required so the ViewLoadManager can handle the rest targetView.Loaded += (sender, e) => Loaded?.Invoke(this, EventArgs.Empty); targetView.Unloaded += (sender, e) => Unloaded?.Invoke(this, EventArgs.Empty); TargetView.DataContextChanged += OnTargetViewDataContextChanged; Log.Debug("Subscribing to view properties"); // This also subscribes to DataContextChanged, don't double subscribe var viewPropertiesToSubscribe = DetermineInterestingViewProperties(); foreach (var viewPropertyToSubscribe in viewPropertiesToSubscribe) { TargetView.SubscribeToPropertyChanged(viewPropertyToSubscribe, OnTargetViewPropertyChanged); } Log.Debug($"Constructed behavior '{GetType().Name}' for '{TargetViewType?.Name}'"); }
/// <summary> /// Initializes a new instance of the <see cref="LogicBase"/> class. /// </summary> /// <param name="targetView">The target control.</param> /// <param name="viewModelType">Type of the view model.</param> /// <param name="viewModel">The view model.</param> /// <exception cref="ArgumentNullException">The <paramref name="targetView"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="viewModelType"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="viewModelType"/> does not implement interface <see cref="IViewModel"/>.</exception> protected LogicBase(IView targetView, Type viewModelType = null, IViewModel viewModel = null) { Argument.IsNotNull("targetView", targetView); if (viewModelType == null) { viewModelType = (viewModel != null) ? viewModel.GetType() : _viewModelLocator.ResolveViewModel(targetView.GetType()); if (viewModelType == null) { var error = string.Format("The view model of the view '{0}' could not be resolved. Make sure to customize the IViewModelLocator or register the view and view model manually", targetView.GetType().GetSafeFullName()); Log.Error(error); throw new NotSupportedException(error); } } UniqueIdentifier = UniqueIdentifierHelper.GetUniqueIdentifier <LogicBase>(); Log.Debug("Constructing behavior '{0}' for '{1}' with unique id '{2}'", GetType().Name, targetView.GetType().Name, UniqueIdentifier); TargetView = targetView; ViewModelType = viewModelType; ViewModel = viewModel; #if SL5 Catel.Windows.FrameworkElementExtensions.FixUILanguageBug((System.Windows.FrameworkElement)TargetView); #endif ViewModelBehavior = (viewModel != null) ? LogicViewModelBehavior.Injected : LogicViewModelBehavior.Dynamic; if (ViewModel != null) { SetDataContext(ViewModel); } // Very impoortant to exit here in design mode if (CatelEnvironment.IsInDesignMode) { return; } Log.Debug("Subscribing to view events"); ViewLoadManager.AddView(this); this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, "ViewLoading", OnViewLoadedManagerLoading); this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, "ViewLoaded", OnViewLoadedManagerLoaded); this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, "ViewUnloading", OnViewLoadedManagerUnloading); this.SubscribeToWeakGenericEvent <ViewLoadEventArgs>(ViewLoadManager, "ViewUnloaded", OnViewLoadedManagerUnloaded); // Required so the ViewLoadManager can handle the rest targetView.Loaded += (sender, e) => Loaded.SafeInvoke(this); targetView.Unloaded += (sender, e) => Unloaded.SafeInvoke(this); TargetView.DataContextChanged += OnTargetViewDataContextChanged; Log.Debug("Subscribing to view properties"); // This also subscribes to DataContextChanged, don't double subscribe var viewPropertiesToSubscribe = DetermineInterestingViewProperties(); foreach (var viewPropertyToSubscribe in viewPropertiesToSubscribe) { TargetView.SubscribeToPropertyChanged(viewPropertyToSubscribe, OnTargetViewPropertyChanged); } Log.Debug("Constructed behavior '{0}' for '{1}'", GetType().Name, TargetView.GetType().Name); }