WindowContentType for PopupWindowAction
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:
- The view occupies the memory even if the popup never triggers.
- The view is initialized when the containing xaml is loaded, not when the popup triggers. There may be differences in program state.
- 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).
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 Bad idea. View models should know about views as little as possible, if anything. They definitely should not contain views.
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 :),
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.
I got it, thank you Brian
@brianlagunas But what about my suggestion?
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?
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.
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??
@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.
Honestly, I would use a custom dialog service instead of the PopupWindowAction.
@dvorn have you had any success implementing this in your app? Have you seen any side effects?
@brianlagunas I am using it for years. No visible performance issues.
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.
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?
I think that makes sense.
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.