DialogManager idea
#15949
Replies: 1 comment
-
It's a bit minimal, but works well enough. My dialog service, which I call an (user) interaction service, has 3 flows:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I created some simple classes to show dialogs in my MVVM app. I wanted to run it by the community to see what yall thoughts were.
This is how I am handling opening and responding to sub windows, I call them dialog in our library.
I created a derived class from the ViewModelBase that is part of the avalonia template called DialogViewModel base.
It has an event called CloseRequested as well as some RelayCommands to close the dialog with or without a result.
I then created a Window as a generic host for dialogs. It contains a single ContentControl bound to a DialogContent property. In order for this window to show the correct view you need a ViewLocator data template set up to provide views based on viewmodels. I am using the reflection based one from the examples.
In the code behind of this Window I connect the click event to the CloseRequested? event of the DialogVeiwModelBase.
To manage opening the dialogs from the parent view model I have a DialogManager class. I just made it a property of the my App but you could use a service if you wanted to go more MVVM.
So all together the way this works is, a command in a parent viewmodel will use the DialogManager to open a dialog with the supplied DialogViewModelBase. Then that dialog binds to one of the provided Close RelayCommands. The DialogHost window (or flyout base) that is showing the dialog then responds to the event and closes the window when requested. The parent viewmodel can then check the response of the dialog's result property.
Here's an example of using it in a parent viewmodel to open a window to select a file to open.
With this approach the viewmodels do not know anything about the views and I limit the view interaction with the view model to within the DialogHost class.
I didn't include it for clarity but I have a little more logic to align this window to a supplied control or with a specific edge of the parent window.
Beta Was this translation helpful? Give feedback.
All reactions