Is the WPF Prism framework Region invalid?

Is the WPF Prism framework Region invalid?

The general operation process for general client projects is: popup login window ="account verification successful =" close login window ="popup main window =" work in main window.

最后更新 1/7/2021 10:53 AM
沙漠尽头的狼
预计阅读 5 分钟
分类
WPF
专题
WPF MVVM Framework Prism Series
标签
.NET WPF Prism Region

站长 15 年开始使用Prism 4,去年(2020 年 😊)也使用Prism 8做开源项目,今天分享处理Prism Region的一个问题。

problem description

The general operation process for general client projects is: popup login window ="account verification successful =" close login window ="popup main window =" work in main window.

General login process

常规登录流程

Like the main gif form above, there is a tree on the left and TabControl on the right, using the injected view code in the Prism module:

public class ModuleOfLogModule : IModule
	{

		public void RegisterTypes(IContainerRegistry containerRegistry)
		{
			containerRegistry.RegisterForNavigation<MainTabItemView, MainTabItemViewModel>(KEY_OF_CURRENT_MODULE);
		}
	}

Main project TabControl displays the module view area:

<TabControl
  prism:RegionManager.RegionName="{x:Static inf:RegionNames.MainTabRegion}"
/>

When you click the menu tree on the left, the dynamic navigation module view:

private void RaiseSelectedItemHandler(CustomMenuItem menuItem)
{
  // 此处省略N多代码
  region.RequestNavigate(menuItem.Key);
  // 此处省略N多代码
}

During actual operation, it was found that navigation did not work. The original operation was to login successfully, and the main form of New popped up directly. The login form view registered in app.xaml.cs:

protected override Window CreateShell()
{
  return Container.Resolve<LoginView>();
}

Someone on Baidu also encountered this problem:

  1. Under the WPF Prism framework, log in to the form first and then open the main form (CSDN discussion posts, the original link is no longer maintained)

The discussion area was very popular and I didn't see the desired results.

  1. prism – 区域管理器无法在自定义弹出窗口中找到区域

The answer given in this article is to manually re-register the zone manager, but the webmaster did not use it.

RegionManager.SetRegionName( theNameOfTheContentControlInsideThePopup, WellKnownRegionNames.DataFeedRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideThePopup, theRegionManagerInstanceFromUnity );
  1. Prism MVVM 应用 登录后切换主窗体实现

This code uses login and main form as user controls, registers shellview in app.xaml.cs, and sets an area in shellview. The two user controls switch in this area through navigation. The effect is no problem, and the area in the main form can be used normally. However, the customized login interface and main interface are generally different in title bars. This practice is quite troublesome and is not recommended.

看问题 3 类似的描述:Prism MVVM 应用 登录后切换主窗体实现

应用场景
    使用Prism7开发WPF程序,编码采用MVVM形式。当程序启动时,首先进入一个登录界面,进行登录认证,认证成功后转入程序布局主窗口。

设计思路
    WPF程序框架搭建后,程序中存一个Shell.xaml,相当于表演者的唯一舞台。登录窗体(以下简称 LoginView)和程序布局主窗体(以下简称 MainView),分别利用IRegionManager进行管理,根据需要在不同时机相继出场表演。所有操作均由各自ViewModel(简称VM)代码完成。
    1.当程序启动后,Shell通过VM,使用RegionManager的Add方法激活LoginView,(注:站长补充描述=登录验证成功,注销LoginView,再通过Add方法激活MainView)

Solutions used by webmasters

Baidu basically couldn't find a more suitable solution, and this problem has been entangled with me for several days (I work for 2 or 3 hours every night, and the webmaster no longer works WPF).

还好我有科学上网的方法,在 YouTube 上Adding a Prism Login Screen找到一个答案。

Adding a Prism Login Screen

解决方案的代码很简单,在 app.xaml.cs 中添加如下代码,在初始化 shell 之前(InitializeShell,shellCreateShell()注册的主窗体),先弹出登录窗口,验证成功再初始化 shell(base.InitializeShell(shell)):

protected override void InitializeShell(Window shell)
{
  LoginView loginView = new LoginView();
  if (loginView.ShowDialog() == true)
  {
    var shellVM = shell.DataContext as MainWindowViewModel;
    shellVM.InitData();
    base.InitializeShell(shell);
  }
  else
  {
    Application.Current.Shutdown(-1);
  }
}

Discussion at the end of the paper

其实该解决方案还是有问题的,在调用InitializeShell(Window shell)之前,站长调试发现模块视图已经执行了初始化,按道理说应该是登录成功后模块才执行初始化的,更多思考留给你,有什么建议欢迎Dotnet9网站留言。

Keep Exploring

延伸阅读

更多文章