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

时间:2019-06-01 16:43:06

标签: c# wpf prism

在对“区域导航”在Prism中的工作方式进行有效的理解时,我遇到了一些麻烦。我正在尝试创建一个基于MVVM的应用程序,该应用程序可加载主窗口并显示由登录表单生成的视图。提交登录表单后,我想导航到新的UserControl。我想知道如果不使用模块也是可以的,但是对于当前的实现,它是模块化的。

使用此当前代码,将显示带有按钮的菜单栏,但不会显示Login视图。

主模块

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:

<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

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

登录模块

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:

<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>

1 个答案:

答案 0 :(得分:0)

  

我想知道在不使用模块的情况下是否也可以实现

可以肯定。模块是完全可选的,如果愿意,您可以从bootstrapper / PrismApplication中进行所有注册。

像这样的东西没什么问题:

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

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