无法从Xamarin Forms ios应用程序中的根页面导航到页面

时间:2019-01-08 06:44:18

标签: c# xamarin.forms xamarin.ios prism

我有一个使用Prism开发的Xamarin表单应用程序。 我在IOS中面临的问题是我无法从下面的方法导航到CreateAccount页面。但是相同的代码在UWP中可以正常工作。

  protected async override void OnInitialized()
    {
        InitializeComponent();            
        await NavigationService.NavigateAsync($"NavigationPage/LoginPage");
    }

 protected override void RegisterTypes(IContainerRegistry 
 containerRegistry)
    {
     containerRegistry.RegisterForNavigation<CreateAccount, CreateAccountViewModel>();            
   containerRegistry.RegisterForNavigation<LoginPage,LoginViewModel>();
   containerRegistry.RegisterForNavigation<ForgotPassword>();
   }

当我像下面这样替换上面的行时,正在发生导航,但未注册ViewModel。

 containerRegistry.RegisterForNavigation<CreateAccount>(); 

AppDelegate.cs

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{        
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App(new iOSInitializer()));

        return base.FinishedLaunching(app, options);
    }

}

public class iOSInitializer : IPlatformInitializer
{

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {            
    }
}
}

CreateAccountPageViewModel:

public class CreateAccountViewModel : BasePageViewModel, 
INavigationAware
 {  
  public static INavigationService _navigationService;

  public CreateAccountViewModel(INavigationService navigationService)
        : base(navigationService)
     {
    _navigationService = navigationService;
    }
    } 

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

解决方案:

尝试在NavigationPage中注册RegisterTypes

  protected override void RegisterTypes(IContainerRegistry
containerRegistry)
        {
            containerRegistry.RegisterForNavigation<CreateAccount, CreateAccountViewModel>();
            containerRegistry.RegisterForNavigation<LoginPage, LoginViewModel>();
            containerRegistry.RegisterForNavigation<ForgotPassword>();

            //add
            containerRegistry.RegisterForNavigation<NavigationPage>();
        }