Question

I'm currently using Prism's InteractionRequest to display new windows. I use them for simple confirmations as well as displaying a window window with a custom view/viewmodel, following the sample here. Anyway, in all of these cases, I display the window and some button on the window is responsible for closing it. I'd like to display a window and have the object that called it be responsible for closing it.

Here is my implementation:

ActionNotification

public abstract class ActionNotification: Notification, INotifyPropertyChanged, IPopupWindowActionAware
{
    public event PropertyChangedEventHandler PropertyChanged;

    // IPopupWindowActionAware 
    public System.Windows.Window HostWindow { get; set; } // Set when the "action" in the view is triggered
    public Notification HostNotification { get; set; } // Set when the "action" in the view is triggered

    public ActionNotification(string content)
    {
        this.Content = content;
    }

    public void CompleteAction()
    {
        if (this.HostWindow != null)
        {
            this.HostWindow.Close();
        }
    }

    // INotifyPropertyChange implementation
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Calling method

/// <summary>
/// Pushes a unit of work onto a separate thread and notifies the view to display an action notification
/// </summary>
/// <param name="actionNotification">The notification object for the view to display</param>
/// <param name="act">The unit of work to perform on a separate thread</param>
private void DoWorkAndRaiseAction(ActionNotification actionNotification, Action act)
{
    Task.Factory.StartNew(() =>
    {
        try
        {
            act();
        }
        finally
        {
            Application.Current.Dispatcher.Invoke((Action)(() => actionNotification.CompleteAction()));
        }
    });

    ActionInteractionReq.Raise(actionNotification);
}

This all works well but it appears that I would be suck if the "work" completed before I was able to raise the InteractionRequest. Can anyone offer some advice to GUARANTEE either the work hasn't completed before raising the request otherwise don't raid the request?

EDIT: I should add that the window is being shown as modal, so no code is executed after the request is raised, which is why I push the work off onto a separate task

EDIT2: Here is how the view interacts with the request:

<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding Path=ActionInteractionReq, Mode=OneWay}">
        <int_req:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStyle="None" WindowHeight="150" WindowWidth="520">
            <int_req:PopupWindowAction.WindowContent>
                <int_req:ZActionNotificationView/>
            </int_req:PopupWindowAction.WindowContent>
        </int_req:PopupWindowAction>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

When Raise is called, the PopupWindowAction is triggered and creates a new Window. It then does ShowDialog on that window.

EDIT3: From the advice from the comments, I've included the PopupWindowAction. I've cut out some irrelevant code for the sake of brevity

public class PopupWindowAction : TriggerAction<FrameworkElement>
{
    /*      
       Here is where a few dependency properties live that dictate things like Window size and other stuff, e.g.

        /// <summary>
        /// Determines if the content should be shown in a modal window or not.
        /// </summary>
        public static readonly DependencyProperty IsModalProperty =
            DependencyProperty.Register(
                "IsModal",
                typeof(bool),
                typeof(PopupWindowAction),
                new PropertyMetadata(null));        
    */

    /* 
        Here is where the accessors live for the DPs, e.g.

        /// <summary>
        /// Gets or sets if the window will be modal or not.
        /// </summary>
        public bool IsModal
        {
            get { return (bool)GetValue(IsModalProperty); }
            set { SetValue(IsModalProperty, value); }
        }
    */

    #region PopupWindowAction logic

    /// <summary>
    /// Displays the child window and collects results for <see cref="IInteractionRequest"/>.
    /// </summary>
    /// <param name="parameter">The parameter to the action. If the action does not require a parameter, the parameter may be set to a null reference.</param>
    protected override void Invoke(object parameter)
    {
        var args = parameter as InteractionRequestedEventArgs;
        if (args == null)
        {
            return;
        }

        // If the WindowContent shouldn't be part of another visual tree.
        if (this.WindowContent != null && this.WindowContent.Parent != null)
        {
            return;
        }

        var wrapperWindow = this.GetWindow(args.Context); // args.Context here is the Notification object I'm passing to the InteractionRequest

        var callback = args.Callback;
        EventHandler handler = null;
        handler =
            (o, e) =>
            {
                wrapperWindow.Closed -= handler;
                wrapperWindow.Owner = null;
                wrapperWindow.Content = null;
                callback();
            };
        wrapperWindow.Closed += handler;

        if (this.IsModal)
        {
            wrapperWindow.ShowDialog();
        }
        else
        {
            wrapperWindow.Show();
        }
    }

    /// <summary>
    /// Checks if the WindowContent or its DataContext implements IPopupWindowActionAware and IRegionManagerAware.
    /// If so, it sets the corresponding values.
    /// Also, if WindowContent does not have a RegionManager attached, it creates a new scoped RegionManager for it.
    /// </summary>
    /// <param name="notification">The notification to be set as a DataContext in the HostWindow.</param>
    /// <param name="wrapperWindow">The HostWindow</param>
    protected void PrepareContentForWindow(Notification notification, Window wrapperWindow)
    {
        if (this.WindowContent == null)
        {
            return;
        }

        // We set the WindowContent as the content of the window. 
        wrapperWindow.Content = this.WindowContent;

        /* Code removed for brevity */

        // If the WindowContent implements IPopupWindowActionAware, we set the corresponding values.
        IPopupWindowActionAware popupAwareContent = this.WindowContent as IPopupWindowActionAware;
        if (popupAwareContent != null)
        {
            popupAwareContent.HostWindow = wrapperWindow;
            popupAwareContent.HostNotification = notification;
        }

        // If the WindowContent's DataContext implements IPopupWindowActionAware, we set the corresponding values.
        IPopupWindowActionAware popupAwareDataContext = this.WindowContent.DataContext as IPopupWindowActionAware;
        if (popupAwareDataContext != null)
        {
            popupAwareDataContext.HostWindow = wrapperWindow;
            popupAwareDataContext.HostNotification = notification;
        }
    }

    #endregion

    #region Window creation methods

    /// <summary>
    /// Returns the window to display as part of the trigger action.
    /// </summary>
    /// <param name="notification">The notification to be set as a DataContext in the window.</param>
    /// <returns></returns>
    protected Window GetWindow(Notification notification)
    {
        Window wrapperWindow;

        if (this.WindowContent != null)
        {
            wrapperWindow = new Window();
            wrapperWindow.WindowStyle = this.WindowStyle;
            // If the WindowContent does not have its own DataContext, it will inherit this one.
            wrapperWindow.DataContext = notification;
            wrapperWindow.Title = notification.Title ?? string.Empty;               

            this.PrepareContentForWindow(notification, wrapperWindow);
        }
        else
        {
            wrapperWindow = this.CreateDefaultWindow(notification);
            wrapperWindow.DataContext = notification;
        }

        return wrapperWindow;
    }

    private Window CreateDefaultWindow(Notification notification)
    {
        return new DefaultNotificationWindow 
        { 
            NotificationTemplate = this.ContentTemplate, 
            MessageBoxImage = GetImageFromNotification(notification as ZBaseNotification) 
        };
    }        

    #endregion
}
Was it helpful?

Solution

The underlying issue here is that the code that starts the async operation and the code that displays the window are just not cooperating. The design based on IPopupWindowActionAware is IMHO not very good; pushing property values around is OK for common scenarios, but here it starts showing its limitations.

Let's first consider a localized solution that works with the current code:

public Window HostWindow { /* call OnPropertyChanged! */ }

public void CompleteAction()
{
    if (this.HostWindow != null)
    {
        this.HostWindow.Close();
    }
    else
    {
        this.PropertyChanged += (o, e) => {
            if (e.PropertyName == "HostWindow" && this.HostWindow != null)
            {
                var hostWindow = this.HostWindow; // prevent closure-related bugs

                // kill it whenever it appears in the future
                hostWindow.Loaded += (o, e) => { hostWindow.Close(); };

                // kill it right now as well if it's been shown already
                // (we cannot assume anything)
                if (hostWindow.IsLoaded)
                {
                    hostWindow.Close();
                }
            }
        };
    }
}

This is not quite elegant, but it does the job: if CompleteAction is called before the window is known, then when the window becomes known we attach a handler that closes it immediately whenever it get displayed. The double-deep event handler assignment is necessary because the window might not be shown at the time it becomes known to us.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top