Wpf 请求在MainWindowViewModel中导航

Wpf 请求在MainWindowViewModel中导航,wpf,prism,Wpf,Prism,嗨,我想当应用程序运行时,应用程序自动导航到视图,所以我使用如下方式: protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) {

嗨,我想当应用程序运行时,应用程序自动导航到视图,所以我使用如下方式:

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

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<MainContent>();

            containerRegistry.RegisterForNavigation<Subscene>();
        }
此代码不起作用,没有错误,也没有导航

我的区域存在于另一个名为
MainContent
的用户控件中。这只适用于“普通”视图模型,不适用于shell的视图模型,因为该视图模型创建得太早。您应该在初始化中执行初始导航

internal class MyApp : PrismApplication
{
     // ...

     protected override void OnInitialized()
     {
         base.OnInitialized();
         Container.Resolve<IRegionManager>().RequestNavigate("ContentRegion", "Subscene", myparameter);
     }

     // ...
}
内部类MyApp:PrismApplication
{
// ...
受保护的覆盖无效OnInitialized()
{
base.OnInitialized();
Container.Resolve().RequestNavigate(“ContentRegion”、“Subscene”、myparameter);
}
// ...
}
不相关的旁注:如果您在构造函数中使用参数而不是字段,Resharper将告诉您在将来的重构中何时不再需要该字段。

这仅适用于“普通”视图模型,而不适用于shell的视图模型,因为该模型创建得太早。您应该在初始化中执行初始导航

internal class MyApp : PrismApplication
{
     // ...

     protected override void OnInitialized()
     {
         base.OnInitialized();
         Container.Resolve<IRegionManager>().RequestNavigate("ContentRegion", "Subscene", myparameter);
     }

     // ...
}
内部类MyApp:PrismApplication
{
// ...
受保护的覆盖无效OnInitialized()
{
base.OnInitialized();
Container.Resolve().RequestNavigate(“ContentRegion”、“Subscene”、myparameter);
}
// ...
}
不相关的旁注:如果在构造函数中使用参数而不是字段,Resharper将告诉您在将来的重构中何时不再需要该字段