/// <summary> /// Called when the <see cref="TargetWindow"/> has been closed. /// </summary> /// <remarks> /// Public to allow the generated ILGenerator to access this method. /// </remarks> // ReSharper disable UnusedMember.Local public void OnTargetWindowClosed() // ReSharper restore UnusedMember.Local { #if SILVERLIGHT // This code is implemented due to a bug in the ChildWindow of silverlight, see: // http://silverlight.codeplex.com/workitem/7935 // Only handle this once if (_isClosed) { return; } _isClosed = true; #endif if (_closeInitiatedByViewModel == null) { _closeInitiatedByViewModel = false; bool?dialogResult = null; if (!PropertyHelper.TryGetPropertyValue(TargetWindow, "DialogResult", out dialogResult)) { Log.Warning("Failed to get the 'DialogResult' property of window type '{0}', using 'null' as dialog result", TargetWindow.GetType().Name); } CloseViewModel(dialogResult); } _dynamicEventListener.UnsubscribeFromEvent(); }
/// <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; }
/// <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; } }
/// <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; }
/// <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; } } }