简体   繁体   English

mvvmcross WPF 显示用户对话框

[英]mvvmcross WPF show user dialog

How can I show a Loading-Dialog in my MvvmCross Application?如何在我的 MvvmCross 应用程序中显示加载对话框?

At first, i did it like so( MvvmCross standard );起初,我是这样做的( MvvmCross 标准);

[MvxWindowPresentation(Modal = true)]
public partial class LoadingView : MvxWindow
{
    public LoadingView () => InitializeComponent();
}

and whenever i needed the LoadingDialog;每当我需要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.这看起来很奇怪,因为模态视图是一个新的 window,但我想在我的主应用程序中实现覆盖。

Second, tried it with a normal User Control and the MaterialDesignThemes nugget ;其次,用普通的 User Control 和MaterialDesignThemes nugget进行了尝试;

public partial class LoadingView : UserControl
{
    public LoadingView () => InitializeComponent();
}

and whenever i needed the LoadingDialog;每当我需要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.这不起作用,因为我认为必须先在 Mvx.IoCprovider 中注册MaterialDesignThemes.Wpf.DialogHost

The DialogHost does not need to be registered. DialogHost不需要注册。 When you place a dialog host instance in XAML like below, the dialog instance will be registered automatically.当您像下面这样在 XAML 中放置对话主机实例时,对话实例将自动注册。

<materialDesign:DialogHost>
   <materialDesign:DialogHost.DialogContent>
      <!-- ...dialog content -->
   <materialDesign:DialogHost.DialogContent>
   <!-- ...content -->
</materialDesign:DialogHost>

Internally, the dialog hosts are tracked in a static HashSet .在内部,对话主机在 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. DialogHost实例在其在 XAML 中的Loaded事件被触发并在Unloaded事件发生时取消注册时注册,如下面的参考源所示。 The InvalidOperationException ( No loaded DialogHost instances. ) exception is only thrown if, there are no loaded instances of DialogHost .只有在没有加载的 DialogHost 实例的情况下才会引发InvalidOperationException (未加载DialogHost No loaded DialogHost instances. )异常。

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.换句话说, Show方法会引发异常,因为您在 XAML 标记中的DialogHost控件尚未加载且未触发Loaded事件或已再次Unloaded的地方调用它。 Consequently, you have to make sure that the dialog is loaded before calling Show , see a similar issue here .因此,您必须确保在调用Show之前已加载对话框,请参阅此处的类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM