How to pop up a dialog box in Prism Module?

How to pop up a dialog box in Prism Module?

Some netizens asked for a request. How to pop up a dialogue box in Prism Module? Like the About dialog box popped up on the main interface?

最后更新 4/14/2021 5:32 PM
沙漠尽头的狼
预计阅读 5 分钟
分类
WPF
专题
WPF MVVM Framework Prism Series
标签
.NET WPF Prism Module

Some netizens asked for a request. How to pop up a dialogue box in Prism Module? Like the About dialog box popped up on the main interface?

网友需求

About window pop-up effects:

The effect is average, and if there is a designer, it can be done beautifully.

This article is not intended to be written in great detail. For specific methods, you can see the source code (attached a link to the warehouse at the end of the article). In addition, you can refer to the following article for learning about Prism:

  1. NET Core 3.x WPF MVVM Framework Prism Series (common to future versions)
https://dotnet9.com/album/wpf-prism
  1. WPF Enterprise Development Framework Building Guide (Revelation), 2020 From Getting Started to Giving Up
https://jhrs.com/2020/37391.html

1 About how does the dialog box pop up?

1.1 Common dialog box form registration

App.xaml.cs

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
  ...
  containerRegistry.RegisterDialogWindow<DialogWindow>();//注册自定义对话框窗体
  ...
}

如上代码,RegisterDialogWindow为注册对话框窗体代码,即所有弹出的对话框是包裹在DialogWindow内的,代码简单我就不贴了,知道他是一个Window就好,具体请看源码:DialogWindow

1.2 Actual dialog box registration

也是在 App.xaml.cs 文件中,下面的代码即为注册实际的对话框代码:QQ群对话框关于对话模式推荐网站对话框等,括号中的字符串(如"About")用于弹出对话框定位使用,注:实际的对话框是基于UserControl编写的,只有这样才能套入是一个WindowRegisterDialogWindow

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
  ...
  containerRegistry.RegisterDialogWindow<DialogWindow>();//注册自定义对话框窗体
  containerRegistry.RegisterDialog<QQGroupView, QQGroupViewModel>("QQGroup");
  containerRegistry.RegisterDialog<AboutView, AboutViewModel>("About");
  containerRegistry.RegisterDialog<WebView, WebViewModel>("Web");
  ...
}

This is even simpler. It can be used directly in xaml, such as the About menu in the main window:

<menuitem
  Command="{Binding RaiseShowDialogCommand}"
  CommandParameter="About"
  Header="{markup:I18n {x:Static i18NResources:Language.About}}"
>
  <MenuItem.Icon>
    <Path
      Width="16"
      Data="{StaticResource InfoGeometry}"
      Fill="{DynamicResource SuccessBrush}"
    />
  </MenuItem.Icon>
</menuitem>

RaiseShowDialogCommand在 VM 的基类ViewModelBase定义的,实际调用代码如下:

/// <summary>
/// 显示对话框
/// </summary>
/// <param name="dlgName"></param>
private void RaiseShowDialogHandler(string dlgName)
{
  DialogService.ShowDialog(dlgName);
}

参数dlgName即上面1.2注册对话框时指定的字符串,视情况指定。

2 How do the dialog box pop up in Module?

This section is the focus of this article.

2.1 Key: IModule's RegisterTypes method

Module是动态加载的,模块中的对话框不可能写在App.xaml.cs中噻?如果你对Prism比较熟悉,看看各模块的Module入口类,即继承IModule的模块入口类方法RegisterTypes(IContainerRegistry containerRegistry),可以在这里注入模块使用的类型和对话框,供本模块或者其他模块使用。

2.2 Implementation of module dialog box

LQClass.ModuleOfLog模块为例,我们在下面注册添加日志对话框:

public void RegisterTypes(IContainerRegistry containerRegistry)
{
  ...
  containerRegistry.RegisterDialog<AddView, AddViewModel>("AddLogView");
  ...
}

AddView为添加日志视图,和一般用户控件没有区别,可参考关于对话框编写,本项目视图链接见AddView.xaml

2.3 How to call it?

调用方式同主窗口菜单一致,代码文件路径:src\LQClass.AdminForWPF\Modules\LQClass.ModuleOfLog\Views添加按钮声明如下:

<Button
  Margin="10,15,10,0"
  Style="{StaticResource ButtonSuccess}"
  Command="{Binding RaiseShowDialogCommand}"
  CommandParameter="AddLogView"
  Content="{markup:I18n {x:Static i18NResources:Language.Add}}"/>

命令参数AddLogView2.2中注册视图时给的名称。

The effect is as follows. It only demonstrates how to pop up a dialog box in the module, without adding actual business:

3 How does the project work?

说明:本项目服务端基于WTM搭建,客户端由 WPF 编写。

3.1 Running server

如果需要运行查看客户端效果,请先编译服务端src\LQClass.Admin,至于怎么编译,不会的自行百度哦,编译成功后,调试运行服务端项目,或者双击运行服务端 Exe:src\LQClass.Admin\LQClass.Admin\bin\Debug\net5.0\LQClass.Admin.exe,服务端默认开启 5000 端口。

3.2 Run the WPF client

If you just look at the source code, you can ignore this step.

注意查看App.config,客户端连接服务端的地址是否正确:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<appSettings>
    ...
		<add key="API" value="http://localhost:5000/api/"/>
	</appSettings>
  ...
</configuration>

项目路径:src\LQClass.AdminForWPF,生成成功,调试客户端项目,或者双击客户端 Exe:\src\LQClass.AdminForWPF\Build\LQClass.AdminForWPF.exe即可运行本客户端了。

Back-end default administrator account:

admin
000000

Finally, let's give a complete demonstration:

Warehouse (welcome to mention issues): https://www.example.com

Keep Exploring

延伸阅读

更多文章