PRISM导航到另一个视图

时间:2015-10-05 08:48:36

标签: c# wpf mvvm navigation prism

我有一个MainWindow,它只包含显示其他视图的区域:

<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion"/>

我要做的是,在MainWindowViewModel加载后立即导航到MainPageViewModel

我尝试实现接口INavigationAware,如下所示:

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        _regionManager.RequestNavigate("ContentRegion", App.Experiences.DetailPage.ToString());
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
        _regionManager.RequestNavigate("ContentRegion", App.Experiences.DetailPage.ToString());
    }

但即使我在这些断点上设置断点,它们也永远不会被执行。

我做错了什么?

修改

也许我需要更改我的Bootstrapper逻辑?这是它的样子:

public class Bootstrapper: UnityBootstrapper
{
    protected override System.Windows.DependencyObject CreateShell()
    {
        return Container.TryResolve<MainWindow>();
    }
    protected override void InitializeShell()
    {
        Application.Current.MainWindow.Show();

    }
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();

        Container.RegisterType(typeof(IDataRepository), typeof(DataRepository), null,new Microsoft.Practices.Unity.ContainerControlledLifetimeManager());


        Container.RegisterTypeForNavigation<MainPage>(App.Experiences.MainPage.ToString());
        Container.RegisterTypeForNavigation<DetailPage>(App.Experiences.DetailPage.ToString());


    }
}

1 个答案:

答案 0 :(得分:1)

我必须编辑我的Bootstrapper逻辑,如下所示:

    protected override void InitializeShell()
    {
        Application.Current.MainWindow.Show();

        Prism.Regions.IRegionManager newRegion = Container.TryResolve<Prism.Regions.IRegionManager>();
        newRegion.RequestNavigate("ContentRegion", App.Experiences.MainPage.ToString());
    }