ShowDialog forces ownership
See original GitHub issueDescribe the bug
In WinForms and WPF, one can call ShowDialog()
on a window instance without specifying the owner window. The new dialog is displayed on top of all the other opened windows of the application, which all become blocked until the dialog is closed.
To Reproduce
Call ShowDialog()
on a window instance, without specifying a parent window as argument.
Expected behavior As all the other UI frameworks work, a parent owner window should not be mandatory when displaying a dialog. This becomes important in situations like an abstract factory for views used in conjunction with a Dependency Injection system, when the DI system resolves a window from an interface, from a project that has no references to any view-related code, ie: business model in domain driven design. At the moment of using the abstract view factory to display a window, all info regarding window ownership is not available without adding a reference to Avalonia.Controls (where Window type is defined) or hacky workarounds.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
@rusoaica if your app is guaranteed to have only one toplevel window with everything else being a dialog, then it’s fine to keep it as a singleton on the UI layer.
Otherwise I’d suggest to use a technique that tracks active views for a particular view model. For example you could have a base class for your views that would keep a shared
Dictionary<ViewModelBase, List<ViewBase>>
and update its contents inOnAttachedToVisualTree
,OnDetachedFromVisualTree
and onDataContext
changes. That way your UI layer will know which visible controls are currently associated with a particular view model, so you could get the appropriate toplevel Window to use as a parent.@kekekeks the workaround I ended up using is to store the hash code of each window inside its view model, and pass that as argument… when displaying a dialog, I am iterating all opened windows of the application, and set as parent the one that matches the provided hash code. As I said, looks hackish, but works:
For the DI container registration:
After that: