Reputation: 430
How can I show a Loading-Dialog in my MvvmCross Application?
At first, i did it like so(MvvmCross standard);
[MvxWindowPresentation(Modal = true)]
public partial class LoadingView : MvxWindow
{
public LoadingView () => InitializeComponent();
}
and whenever i needed the LoadingDialog;
_navigationService.Navigate<LoadingView>());
This looks really weird because the Modal view is a new window, but i want to achieve a overlay in my main-application.
Second, tried it with a normal User Control and the MaterialDesignThemes nugget;
public partial class LoadingView : UserControl
{
public LoadingView () => InitializeComponent();
}
and whenever i needed the LoadingDialog;
var result = await MaterialDesignThemes.Wpf.DialogHost.Show(new LoadingView ());
This doesnt work, because I think have to register the MaterialDesignThemes.Wpf.DialogHost in the Mvx.IoCprovider before.
Upvotes: 0
Views: 740
Reputation: 22099
The DialogHost
does not need to be registered. When you place a dialog host instance in XAML like below, the dialog instance will be registered automatically.
<materialDesign:DialogHost>
<materialDesign:DialogHost.DialogContent>
<!-- ...dialog content -->
<materialDesign:DialogHost.DialogContent>
<!-- ...content -->
</materialDesign:DialogHost>
Internally, the dialog hosts are tracked in a static HashSet
. A DialogHost
instance is registered when its Loaded
event in XAML is fired and deregistered when the Unloaded
event occurrs, as you can see from the reference source below. The InvalidOperationException
(No loaded DialogHost instances.
) exception is only thrown if, there are no loaded instances of DialogHost
.
private static readonly HashSet<DialogHost> LoadedInstances = new HashSet<DialogHost>();
public DialogHost()
{
this.Loaded += new RoutedEventHandler(this.OnLoaded);
this.Unloaded += new RoutedEventHandler(this.OnUnloaded);
// ...
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
DialogHost.LoadedInstances.Add(this);
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
DialogHost.LoadedInstances.Remove(this);
}
In other words, the Show
method throws an exception, because you call it in places, where the DialogHost
control in your XAML markup is not loaded yet and did not fire the Loaded
event, or it is already Unloaded
again. Consequently, you have to make sure that the dialog is loaded before calling Show
, see a similar issue here.
Upvotes: 1