Prism icon indicating copy to clipboard operation
Prism copied to clipboard

WindowContentType for PopupWindowAction

Open dvorn opened this issue 10 years ago • 17 comments

PopupWindowAction may specify the content of the window

    <prism:InteractionRequestTrigger SourceObject="{Binding CustomPopupViewRequest, Mode=OneWay}">
        <!-- This PopupWindowAction has a custom view defined. When this action is executed the view will be shown inside a new window -->
        <!-- Take into account that the view is created only once and will be reused each time the action is executed -->
        <prism:PopupWindowAction>
            <prism:PopupWindowAction.WindowContent>
                <views:CustomPopupView />
            </prism:PopupWindowAction.WindowContent>
        </prism:PopupWindowAction>
    </prism:InteractionRequestTrigger>

As noted in the comment, the view is created once and this has the following drawbacks:

  1. The view occupies the memory even if the popup never triggers.
  2. The view is initialized when the containing xaml is loaded, not when the popup triggers. There may be differences in program state.
  3. The view may keep some visual state from one invocation to another which may (or may not) be undesirable and may be difficult to overcome from view model.

Suggestion: allow to specify the type of the view instead of the instance of the view. E.g., instead of the above, have

    <prism:InteractionRequestTrigger SourceObject="{Binding CustomPopupViewRequest, Mode=OneWay}">
        <!-- This PopupWindowAction has a custom view defined. When this action is executed the view will be shown inside a new window -->
        <prism:PopupWindowAction WindowContentType ="{x:Type views:CustomPopupView}"/>
    </prism:InteractionRequestTrigger>

Then an instance of the view may be created when needed using ServiceLocator (that is, resolved using container).

dvorn avatar Jan 28 '16 10:01 dvorn

what about binding to a view in Viewmodel? <prism:PopupWindowAction WindowContentType ="{Binding TargetView}"/> where the views are different for each ViewModel is this fix some problems ?

toumir avatar Jan 28 '16 10:01 toumir

@toumir Bad idea. View models should know about views as little as possible, if anything. They definitely should not contain views.

dvorn avatar Jan 28 '16 10:01 dvorn

yes we learned that, the ViewModel should know anything about view, sir Brian Lagunas (Prism.Library manager) always said in his videos training that referencing view in ViewModel it is not a bad idea :),

toumir avatar Jan 28 '16 13:01 toumir

I said that if you are going to reference view properties, or need to perform an action in the View, from a VIewModel then do it via an interface. This way you are not directly referencing view types in a VM because those shouldn't be in there. This way you don't break the MVVM guidelines and are still testable.

For example; if you needed to close a VIew from a VM more easily, instead of creating a custom behavior, you could just do somthign like this:

interface IMyView
{
    void Close();
}

public class MyView : UserControl, IMyView 
{....}

public class MyViewViewModel : BIndableBase
{
     public IMyView View {get;set}

     void CloseView()
     {
          View.Close();
     }
}

As you can see, we are remaining true to the MVVM guidelines, but still giving us a little more flexibility in our VM when it comes to some of the View details. So, View types should not be defined in ViewModels if possible.

brianlagunas avatar Jan 28 '16 14:01 brianlagunas

I got it, thank you Brian

toumir avatar Jan 28 '16 14:01 toumir

@brianlagunas But what about my suggestion?

dvorn avatar Jan 28 '16 15:01 dvorn

It's an interesting idea. I would be a little concerned about the performance hit you would take when showing the dialog. When the dialog is shown, the service locator will have to resolve the content, then set the window's content to that instance. Depending on the complexity of the view, there may be a noticeable lag in showing the dialog. You will also lose the ability to data bind to the properties on your view, but maybe that is a tradeoff of the feature.

You may want to implement this in your own fork and play around with it to see what type of performance issues you run into.

@briannoyes and @bartlannoeye what are your thoughts?

brianlagunas avatar Jan 28 '16 15:01 brianlagunas

I would be a little concerned about the performance hit you would take when showing the dialog. When the dialog is shown, the service locator will have to resolve the content, then set the window's content to that instance. Depending on the complexity of the view, there may be a noticeable lag in showing the dialog.

Not an issue. Exactly that is done when navigating to a view registered with RegisterViewForNavigation. Window's content is set to some instance anyway. Moreover, I suspect that creating and displaying a Window in WPF has a much bigger overhead in itself.

dvorn avatar Jan 28 '16 15:01 dvorn

Is there any way at all to force the view to be reinitialised each time the InteractionRequest is triggered. We have some quite complex views that we absolutely cannot have using the same instance each time.

Being able to pass a type as the WindowContent and having our IOC resolve it each time is the ideal scenario for us, we don't care if this causes a small performance hit.

We would really like the InteractionRequest to work in a similar way to RequestNavigate, in which our IOC always creates us a new instance of our view each time it is called.

Any way to achieve this? Should we even be using InteractionRequest for this??

ICannotDie avatar Aug 19 '16 08:08 ICannotDie

@furyconcept PopupWindowAction is independent of everything else. You can grab the source of it, modify and use in your project - in the view, just reference your variant and not the one from Prism.

Another approach is to use ContentTemplate property (instead of WindowContentType). ContentTemplate was present in the original Damian Cherubini's PopupWindowAction: using custom views instead of windows in WPF and Prism.

dvorn avatar Aug 19 '16 10:08 dvorn

Honestly, I would use a custom dialog service instead of the PopupWindowAction.

brianlagunas avatar Aug 19 '16 16:08 brianlagunas

@dvorn have you had any success implementing this in your app? Have you seen any side effects?

brianlagunas avatar Feb 26 '17 15:02 brianlagunas

@brianlagunas I am using it for years. No visible performance issues.

dvorn avatar Feb 26 '17 16:02 dvorn

Thanks so much for implementing this, it works perfectly for me. Regarding the drawbacks you originally mentioned, the second one had the follow-on effect that if <views:CustomPopupView/> had a view model with constructor arguments, you'd get an annoying designer error (as described here and here).

It was possible to ignore, and things would work at runtime, but having designer errors is not nice and workarounds such as trying to use a region (e.g. <ContentControl prism:RegionManager.RegionName="CustomPopupRegion"/>) came with equally difficult to solve problems.

agoodwin avatar Oct 27 '17 13:10 agoodwin

Thanks @agoodwin. Glad that WindowContentType helped to solve your problem. However,

@brianlagunas Seemingly there is a room for improvement for ViewModelLocator: if CustomPopupView uses AutoWireViewModel, then the AutoWireViewModel property is not set and view model is not instantiated when CustomPopupView is being edited in XAML designer. However, if it is included as element in another view <views:CustomPopupView/> and that view is loaded by the designer, the property is triggering creation of view model and fails.

ViewModelLocator could check for designer mode as is being done for RegionManager.RegionName: https://github.com/PrismLibrary/Prism/blob/master/Source/Wpf/Prism.Wpf/Regions/RegionManager.cs#L106

What do you think?

dvorn avatar Oct 30 '17 05:10 dvorn

I think that makes sense.

brianlagunas avatar Oct 30 '17 14:10 brianlagunas

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 29 '20 11:01 lock[bot]