How can I open/close a new window in WPF without violating rules of the MVVM pattern?
I just want to mimic the login module of ms office outlook.
I've already read this article, but there are an error in passing a parameter confirmation
I'm currently using prism 5.0.
Luckily, Prism 5.0 (and I assume 6.0 too, haven't worked with it yet), has a class called
InteractionRequest<T>
which you can use from code to raise interaction requests.An interaction request is basically a window, that asks the user for a certain action and calls a callback (if necessary or desired) with the users decisions/actions.
In XAML you would do something like
I used my own implemetation of
PopupWindowAction
(see github project page for it's implementation) calledLazyPopupWindowAction
, which will instantiate the embeddedConnectionSettingsView
View on click. If you don't care that your view is instantiated only once, feel free to usePopupWindowAction
, then it will be instantiated at the same time as the View containing the action.It's basically copy & paste with cutting some useless lines from one of my projects. I used
IConfirmation
andINotification
interfaces instead of the concrete implementations.XAML with
PopupWindowAction
Namespace declarations
Update: Since people keep asking about the
LazyPopupWindowAction
, I've put the source in a GitHub Gist. Basically it's based on thePopupWindowAction
from Prims 5 (and for Prism, haven't test it with Prism 6 yet, probably won't work w/o adjustments) and does the exact same thing, but also adds Region and Navigation support with the opened window, something that I needed in my application.One thing I disliked about the default implementation was, that the view and it's viewmodel will be instantiated at the same time the Shell gets instantiated and the ViewModel remains in it's state, when you close it (it was actually just hidden).
Update
What lead me to put another answer was the inability to apply the accepted answer on my project which using the Prism 6,
but after putting the original answer (see it below) and discussing it in comments, I discovered that the core problem was: The Prism 6 changed the namespaces of some classes, all the classes which used in the accepted answer is still exists in Prism 6, but in another dlls and namespaces
So if you are using Prism 6, you can apply the accepted answer with those modifications
first replace those namesapces
with the following namespaces
second update the XAML as the following
NOTE 1
Make sure that the view you are using (in the example above
<views:CustomPopupWindow>
) is NOT a window, or you will receive an exception.NOTE 2
These modifications are required ONLY in case you are using Prism 6. because (As I said in the Original Answer below) the dlls which used by the accepted answer is deprecated in Prism 6.
NOTE 3
Make sure you are referencing the
Prism.Wpf
dll, which could be downloaded from Nuget.Original Answer
The accepted answer is directed to the Prism 5, it uses this library which is deprecated in the Prism 6.
Actually the article which you reference in your question was very helpful (at least for me), and it does not crash.
I will try to summary that article.
ViewModel
XAML
and you will need to use those namespaces
ActionTrigger
Explanation
Prism.Core
andPrism.Wpf
dlls (at least) to make this code work.ShowWindow
method, will trigger theInvoke
method of theShowWindowAction
, which will really show the window.OnWindowClosed
, which we passed it as a callback to theShowWindowAction
class, and we called it from there when the the window really closed.