Ejemplo n.º 1
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();

            string eventName;

            var closedEvent = targetWindowType.GetEventEx("Closed");

            if (closedEvent != null)
            {
                eventName = "Closed";

                _dynamicEventListener = new DynamicEventListener(targetWindow, "Closed", this, "OnTargetWindowClosed");
            }
            else
            {
                eventName = "Unloaded";

                _dynamicEventListener = new DynamicEventListener(targetWindow, "Unloaded", this, "OnTargetWindowClosed");
            }

            _targetWindowClosedEventName = eventName;

            Log.Debug("Using '{0}.{1}' event to determine window closing", targetWindowType.FullName, eventName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles the close subscription.
        /// <para />
        /// The default implementation uses the <see cref="DynamicEventListener"/>.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="data">The data that will be set as data context.</param>
        /// <param name="completedProc">The completed callback.</param>
        /// <param name="isModal">True if this is a ShowDialog request.</param>
        protected virtual void HandleCloseSubscription(object window, object data, EventHandler <UICompletedEventArgs> completedProc, bool isModal)
        {
            var dynamicEventListener = new DynamicEventListener(window, "Closed");

            EventHandler closed = null;

            closed = (sender, e) =>
            {
                bool?dialogResult = null;
                PropertyHelper.TryGetPropertyValue(window, "DialogResult", out dialogResult);

                completedProc(this, new UICompletedEventArgs(data, isModal ? dialogResult : null));
#if SILVERLIGHT
                if (window is ChildWindow)
                {
                    // Due to a bug in the latest version of the Silverlight toolkit, set parent to enabled again
                    // TODO: After every toolkit release, check if this code can be removed
                    Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
                }
#endif
                dynamicEventListener.EventOccurred -= closed;
                dynamicEventListener.UnsubscribeFromEvent();
            };

            dynamicEventListener.EventOccurred += closed;
        }
Ejemplo n.º 3
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();

            string eventName;

            var closedEvent = targetWindowType.GetEventEx("Closed");
            if (closedEvent != null)
            {
                eventName = "Closed";

                _dynamicEventListener = new DynamicEventListener(targetWindow, "Closed", this, "OnTargetWindowClosed");
            }
            else
            {
                eventName = "Unloaded";

                _dynamicEventListener = new DynamicEventListener(targetWindow, "Unloaded", this, "OnTargetWindowClosed");
            }

            _targetWindowClosedEventName = eventName;

            Log.Debug("Using '{0}.{1}' event to determine window closing", targetWindowType.FullName, eventName);
        }
Ejemplo n.º 4
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 (_dynamicEventListener != null)
     {
         _dynamicEventListener.EventOccurred -= OnEventOccurred;
         _dynamicEventListener.UnsubscribeFromEvent();
         _dynamicEventListener = null;
     }
 }
Ejemplo n.º 5
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>
 /// <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>
 public WindowLogic(FrameworkElement targetWindow, Type viewModelType, IViewModel viewModel = null)
     : base(targetWindow, viewModelType, viewModel)
 {
     _dynamicEventListener = new DynamicEventListener(targetWindow, "Closed", this, "OnTargetWindowClosed");
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Handles the close subscription.
        /// <para />
        /// The default implementation uses the <see cref="DynamicEventListener"/>.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="data">The data that will be set as data context.</param>
        /// <param name="completedProc">The completed callback.</param>
        /// <param name="isModal">True if this is a ShowDialog request.</param>
        protected virtual void HandleCloseSubscription(object window, object data, EventHandler<UICompletedEventArgs> completedProc, bool isModal)
        {
            var dynamicEventListener = new DynamicEventListener(window, "Closed");

            EventHandler closed = null;
            closed = (sender, e) =>
            {
                bool? dialogResult = null;
                PropertyHelper.TryGetPropertyValue(window, "DialogResult", out dialogResult);

                completedProc(this, new UICompletedEventArgs(data, isModal ? dialogResult : null));
#if SILVERLIGHT     
                if (window is ChildWindow)
                {
                    // Due to a bug in the latest version of the Silverlight toolkit, set parent to enabled again
                    // TODO: After every toolkit release, check if this code can be removed
                    Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
                }
#endif
                dynamicEventListener.EventOccurred -= closed;
                dynamicEventListener.UnsubscribeFromEvent();
            };

            dynamicEventListener.EventOccurred += closed;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Called when the source has changed.
        /// </summary>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnSourceChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null)
            {
                switch (FocusMoment)
                {
                case FocusMoment.Event:
#if WINDOWS_PHONE
                    throw new NotSupportedInWindowsPhone7Exception();
#else
                    _dynamicEventListener.EventOccurred -= OnSourceEventOccurred;
                    _dynamicEventListener.UnsubscribeFromEvent();
                    break;
#endif

                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 WINDOWS_PHONE
                    throw new NotSupportedInWindowsPhone7Exception();
#else
                    if (string.IsNullOrEmpty(EventName))
                    {
                        throw new InvalidOperationException("Property 'EventName' is required when FocusMode is 'FocusMode.Event'");
                    }

                    _dynamicEventListener = new DynamicEventListener(Source, EventName);
                    _dynamicEventListener.EventOccurred += OnSourceEventOccurred;
                    break;
#endif

                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 == null)
                    {
                        throw new InvalidOperationException("Source does not implement interface 'INotifyfPropertyChanged', either implement it or change the 'FocusMode'");
                    }

                    sourceAsPropertyChanged.PropertyChanged += OnSourcePropertyChanged;
                    break;
                }
            }
        }
Ejemplo n.º 8
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)
        {
            var dynamicEventListener = new DynamicEventListener(window, EventName);

            dynamicEventListener.EventOccurred += OnEventOccurred;
        }
Ejemplo n.º 9
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:
                        
#if WINDOWS_PHONE || NETFX_CORE
                        throw new NotSupportedInPlatformException("Dynamic events are not supported");
#else
                        _dynamicEventListener.EventOccurred -= OnSourceEventOccurred;
                        _dynamicEventListener.UnsubscribeFromEvent();
                        break;
#endif

                    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 WINDOWS_PHONE || NETFX_CORE
                        throw new NotSupportedInPlatformException("Dynamic events are not supported");
#else
                        if (string.IsNullOrEmpty(EventName))
                        {
                            throw new InvalidOperationException("Property 'EventName' is required when FocusMode is 'FocusMode.Event'");
                        }

                        _dynamicEventListener = new DynamicEventListener(Source, EventName);
                        _dynamicEventListener.EventOccurred += OnSourceEventOccurred;
                        break;
#endif

                    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 == null)
                        {
                            throw new InvalidOperationException("Source does not implement interface 'INotifyfPropertyChanged', either implement it or change the 'FocusMode'");
                        }

                        sourceAsPropertyChanged.PropertyChanged += OnSourcePropertyChanged;
                        break;
                }
            }
        }
Ejemplo n.º 10
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>
 /// <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>
 public WindowLogic(FrameworkElement targetWindow, Type viewModelType, IViewModel viewModel = null)
     : base(targetWindow, viewModelType, viewModel)
 {
     _dynamicEventListener = new DynamicEventListener(targetWindow, "Closed", this, "OnTargetWindowClosed");
 }