Prism框架下的登录窗口跳转
Prism框架下的登录窗口跳转思路是,将LoginWindow作为主窗口运行,登录完成后在LoginWindow中使用容器切换窗口为MainWindow并显示,最后关闭LoginWindow即可。
需要注意的是,如果想要在MainWindow中通过区域名称(RegionManager.RegionName)来进行控件注入,那么需要在切换窗口时设置MainWindow的RegionManager,而RegionManager可以通过LoginWindow注入获取(IRegionManager)。
1、App.xaml.cs
public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<LoginWindow>(); } }
2、LoginWindow.xaml.cs
public partial class LoginWindow : Window { private readonly IContainerExtension _container; private readonly IRegionManager _regionManager; public LoginWindow(IContainerExtension container, IRegionManager regionManager) { InitializeComponent(); _container = container; _regionManager = regionManager; if (!DesignerProperties.GetIsInDesignMode(this)) { var viewModel = new LoginWindowViewModel(); viewModel.LoginSuccess += OnLoginSuccess; DataContext = viewModel; } } private void OnLoginSuccess() { var mainWindow = _container.Resolve<MainWindow>(); RegionManager.SetRegionManager(mainWindow, _regionManager); mainWindow.Show(); Close(); } }