Esempio n. 1
0
        /// <summary>
        /// Called when the target object is loaded.
        /// <para />
        /// Note that this method will only be called if the target object is a <see cref="FrameworkElement"/>.
        /// </summary>
        protected override void OnTargetObjectLoaded()
        {
            // CTL-925 Use weak events so unloaded elements (like a ComboBoxItem) can also update. The usage of
            // weak events should prevent memory leaks
            var listener = _onLanguageUpdatedWeakListener;

            if (listener != null)
            {
                if (!ReferenceEquals(listener.Source, TargetObject))
                {
                    listener.Detach();
                    _onLanguageUpdatedWeakListener = null;
                }

                if (!listener.IsSourceAlive)
                {
                    listener.Detach();
                    _onLanguageUpdatedWeakListener = null;
                }
            }

            if (_onLanguageUpdatedWeakListener is null)
            {
                _onLanguageUpdatedWeakListener = this.SubscribeToWeakGenericEvent <EventArgs>(_languageService, "LanguageUpdated", OnLanguageUpdated);
            }

            //_languageService.LanguageUpdated += OnLanguageUpdated;
        }
Esempio n. 2
0
 /// <summary>
 /// Unregisters the handler from the event of the given window.
 /// <code>
 /// protected override void RegisterEventHandler(Window window)
 /// {
 ///     window.Closed += WindowOnClosed;
 /// }
 ///
 /// private void WindowOnClosed(object sender, EventArgs eventArgs)
 /// {
 ///     ExecuteCommand(sender);
 /// }
 /// </code>
 /// </summary>
 /// <param name="window">The window instance the eventhandler has to be unregistered from.</param>
 protected void UnregisterEventHandler(Window window)
 {
     if (_weakEventListener != null)
     {
         _weakEventListener.Detach();
         _weakEventListener = null;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Called when the source has changed.
        /// </summary>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnSourceChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null)
            {
                switch (FocusMoment)
                {
                case FocusMoment.Event:
                    _weakEventListener?.Detach();
                    _weakEventListener = null;
                    break;

                case FocusMoment.PropertyChanged:
                    var sourceAsPropertyChanged = e.OldValue as INotifyPropertyChanged;
                    if (sourceAsPropertyChanged != null)
                    {
                        sourceAsPropertyChanged.PropertyChanged -= OnSourcePropertyChanged;
                    }
                    else
                    {
                        Log.Warning("Cannot unsubscribe from previous source because it does not implement 'INotifyPropertyChanged', this should not be possible and can lead to memory leaks");
                    }
                    break;
                }
            }

            if (e.NewValue != null)
            {
                switch (FocusMoment)
                {
                case FocusMoment.Event:
                    if (string.IsNullOrEmpty(EventName))
                    {
                        throw new InvalidOperationException("Property 'EventName' is required when FocusMode is 'FocusMode.Event'");
                    }

                    _weakEventListener = this.SubscribeToWeakEvent(Source, EventName, OnSourceEventOccurred);
                    break;

                case FocusMoment.PropertyChanged:
                    if (string.IsNullOrEmpty(PropertyName))
                    {
                        throw new InvalidOperationException("Property 'PropertyName' is required when FocusMode is 'FocusMode.PropertyChanged'");
                    }

                    var sourceAsPropertyChanged = e.NewValue as INotifyPropertyChanged;
                    if (sourceAsPropertyChanged is null)
                    {
                        throw new InvalidOperationException("Source does not implement interface 'INotifyfPropertyChanged', either implement it or change the 'FocusMode'");
                    }

                    sourceAsPropertyChanged.PropertyChanged += OnSourcePropertyChanged;
                    break;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WindowLogic"/> class.
        /// </summary>
        /// <param name="targetWindow">The window this provider should take care of.</param>
        /// <param name="viewModelType">Type of the view model.</param>
        /// <param name="viewModel">The view model to inject.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="targetWindow"/> is <c>null</c>.</exception>
        public WindowLogic(IView targetWindow, Type viewModelType = null, IViewModel viewModel = null)
            : base(targetWindow, viewModelType, viewModel)
        {
            var targetWindowType = targetWindow.GetType();

            var closedEvent = targetWindowType.GetEventEx("Closed");
            var eventName   = closedEvent != null ? "Closed" : "Unloaded";

            _targetWindowClosedWeakEventListener = this.SubscribeToWeakGenericEvent <EventArgs>(targetWindow, eventName, OnTargetWindowClosed);

            _targetWindowClosedEventName = eventName;

            Log.Debug("Using '{0}.{1}' event to determine window closing", targetWindowType.FullName, eventName);
        }
Esempio n. 5
0
 /// <summary>
 /// Registers the handler to the event of the given window.
 /// <code>
 /// protected override void RegisterEventHandler(Window window)
 /// {
 ///     window.Closing += WindowOnClosing;
 /// }
 ///
 /// private void WindowOnClosing(object sender, CancelEventArgs cancelEventArgs)
 /// {
 ///     ExecuteCommand(sender);
 /// }
 /// </code>
 /// </summary>
 /// <param name="window">The window instance the eventhandler has to be registered to.</param>
 protected void RegisterEventHandler(Window window)
 {
     _weakEventListener = this.SubscribeToWeakEvent(window, EventName, (Action)OnEventOccurred);
 }