简体   繁体   中英

Navigate to a Default View when Application Loaded using Prism 7 in WPF

I follows samples code provided in https://github.com/PrismLibrary/Prism-Samples-Wpf/blob/master/17-BasicRegionNavigation

I want to achieve the following result when I run the application (without explicitly clicking Navigate to View A). Does anyone know how to do it? 期望的结果

I have tried adding Navigate("ViewA"); after this line . However, I cannot get the desired outcome. Is it because the module hasn't been initialized?

Thanks.

did you add your module to the modulecatalog using override method ConfigureModuleCatalog? take a look at here

Eventually I solve by adding the following code in MainWindow.xaml.cs

public partial class MainWindow
{
    IRegionManager _regionManager;
    public MainWindow()
    {
        InitializeComponent();
        _regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        RegionManager.SetRegionManager(ContentRegion, _regionManager);
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        _regionManager.RequestNavigate("ContentRegion", "ViewA");
    }
}

Get idea from: https://github.com/MahApps/MahApps.Metro/issues/1020#issuecomment-44779574

I'm kinda late to the party here, but I also stumbled over the question of how to navigate to a default view during the applications startup.

I found two ways:

1. App decides the default view

This can be solved in the CreateShell() -override in the App -Class.

This is my CreateShell -Method:

/// <inheritdoc />
protected override Window CreateShell()
{
    var window = this.Container.Resolve<MainWindow>();

    window.Loaded += (sender, args) =>
    {
        var manager = this.Container.Resolve<IRegionManager>();

        manager.RequestNavigate("ContentRegion", "ViewA");
    };

    return window;
}

2. ViewModel decides the default view

Add a constructor to MainWindowViewModel that looks like this:

public MainWindowViewModel(IRegionManager regionManager)
{
    regionManager.RegisterViewWithRegion("ContentRegion", "ViewA");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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