简体   繁体   English

在应用程序启动时加载区域

[英]Load Region On Application Startup

I'm having some trouble gaining a working understanding of how Region Navigation works in Prism. 我很难获得对“区域导航”在Prism中如何工作的有效理解。 I'm trying to create an MVVM based app that loads a main window, and displays a view generated by a login form. 我正在尝试创建一个基于MVVM的应用程序,该应用程序可加载主窗口并显示由登录表单生成的视图。 After the login form is submitted, I then want to navigate to a new UserControl . 提交登录表单后,我想导航到新的UserControl I'd like to know if this is also possible without using modules, however for the current implementation, it's modular. 我想知道如果不使用模块也是可行的,但是对于当前的实现,它是模块化的。

With this current code, the menu bar with a button displays, but not the Login view. 使用此当前代码,将显示带有按钮的菜单栏,但不会显示“ Login视图。

Main Module 主模块

App.xaml.cs App.xaml.cs

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

protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<LoginModule.ModuleLoginModule>();
        }

MainWindow.xaml: MainWindow.xaml:

<Window x:Class="PrismMVVM.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:local="clr-namespace:PrismMVVM"
        mc:Ignorable="d"

        Title="PrismMVVM" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Button Content="Code is Poetry" HorizontalAlignment="Left" Width="Auto"/>
        </DockPanel>
        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion">
        </ContentControl>
    </Grid>
</Window>

MainWindowViewModel.cs MainWindowViewModel.cs

namespace PrismMVVM.ViewModels 
{
    class MainWindowViewModel : BindableBase
    {
        public IRegionManager _regionManager;
        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            regionManager.RequestNavigate("LoginRegion", "Login");
        }
    }
}

Login Module 登录模块

ModuleLoginModule.cs: ModuleLoginModule.cs:

namespace LoginModule 
{
    public class ModuleLoginModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion("LoginRegion", typeof(Login));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<Login>();
        }
    }
}

Login.xaml: Login.xaml:

<UserControl x:Class="LoginModule.Views.Login"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:LoginModule.Views"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="White" prism:RegionManager.RegionName="LoginRegion">
        <StackPanel Panel.ZIndex="1" Margin="150">
            <TextBox HorizontalAlignment="Center" VerticalAlignment="Center">Text</TextBox>
            <PasswordBox HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Button Background="LightBlue" Content="Login" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </StackPanel>

        <Rectangle Panel.ZIndex="0" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="LightGray" Height="300" Width="400" />
    </Grid>
</UserControl>

I'd like to know if this is also possible without using modules 我想知道如果不使用模块也是可行的

For sure it is. 当然可以。 Modules are completely optional, you can do all the registrations from the bootstrapper / PrismApplication , if you like. 模块是完全可选的,如果愿意,您可以从bootstrapper / PrismApplication进行所有注册。

There's nothing wrong with something like this: 像这样的东西没有错:

public class MyApplication : PrismApplication
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<Login>();
    }
}

internal class MainWindowViewModel
{
    public MainWindowViewModel( IRegionManager regionManager )
    {
        regionManager.RequestNavigate( "ContentRegion", "Login" );
    }
}

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

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