xamarin.forms+prism 实现DialogService(对话框服务)

Getting Started with IDialogAware

Creating dialogs with the DialogService requires a ViewModel that is IDialogAware. This special interface is what allows the DialogService to work with your ViewModel. Note that the navigation interfaces such as INavigationAware, IConfirmNavigation, IInitialize, IDestructible are not supported or used by the DialogService. By implementing IDialogAware you have methods that will allow you to do anything that you may be used to doing throught the ViewModel's lifecycle.

使用DialogService创建对话框需要一个IDialogAware的ViewModel。这个特殊的接口允许DialogService与ViewModel一起工作。注意,像INavigationAware, IConfirmNavigation, IInitialize, IDestructible这样的导航接口是不被DialogService支持或使用的。通过实现IDialogAware,你有了一些方法,这些方法将允许你在ViewModel的生命周期中做任何你可能习惯做的事情。

You can provide any specific logic you may want to prevent your dialog from closing. If RequestClose is invoked and CanCloseDialog returns false the Dialog Service will not close the Dialog.

您可以提供任何您想要阻止对话框关闭的特定逻辑。如果RequestClose被调用并且CanCloseDialog返回false,对话服务将不会关闭对话。

Sample Dialog View

Dialog's are built entirely from any Xamarin.Forms Layout or View. These all use normal bindings and might look something like the following:

对话框完全由Xamarin构建。窗体布局或视图。这些都使用普通绑定,可能看起来像下面这样

Registering the Dialog

Similar to registering a Page for Navigation, there is an extension on IContainerRegistry to register your Dialogs. You do not need to specify the ViewModel, as the ViewModelLocator will automatically attempt to resolve the ViewModel as long as both the Dialog and the ViewModel match Prism's naming/namespace conventions. Using the fully qualified registration method in which you specify both the Dialog View and the ViewModel is considered a better practice as this bypasses and Reflection cost that would be incurred by the ViewModelLocator.

类似于注册导航页面,在IContainerRegistry上有一个扩展来注册您的对话框。您不需要指定ViewModel,因为只要Dialog和ViewModel都符合Prism的命名/命名空间约定,ViewModelLocator就会自动尝试解析ViewModel。使用指定对话框视图和ViewModel的完全限定注册方法被认为是一种更好的实践,因为这绕过了ViewModelLocator可能产生的反射成本。

Using the Dialog Service

The Dialog Service is a very simplistic service which has a few overrides to make using it easier.

Dialog Service是一个非常简单的服务,它有一些覆盖,使它更容易使用