Xamarin、Android Activity、IntentFilter、打开表单视图

Xamarin, Android Activity, IntentFilter, Open Forms View

我正在浏览器中进行用户身份验证。

在我成功通过身份验证后,浏览器关闭并执行 AndroidMainActivity.OnCreate()。 但是应用程序显示空白屏幕,如未加载 View/Page。 我从 MVVMCross (MvvmCross.Logging.MvxLog) 获取此日志没有为 LoadViewModel 中的 AndroidMainActivity 指定 ViewModel class。

看来我现在应该导航到某个表单页面??? 但是导航对我没有任何作用。可能我应该以不同的方式来做,但我找不到任何关于如何做的文章或例子。

这就是我现在尝试的方式:

    [Activity(MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    [IntentFilter([] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = GdspScheme.SCHEME)]
    public class AndroidMainActivity : MvxFormsAppCompatActivity<AndroidSetup, MainApplication, App>
    {
        protected override void OnCreate(Bundle bundle)
        {
            if (Intent.Data != null)
            {
                // user authenticated

                Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new NavigationPage(new FormsView()));
            }
        }
    }

当然你应该在 Forms 项目中调用这一行。

您可以使用MessagingCenter

MainActivity

if (Intent.Data != null)
{
  // user authenticated
  MessagingCenter.Send<Object>(this, "authenticatedFinished");
  
}

在Forms中->MainPage的构造函数

public xxxMainPage()
{
  //...
  MessagingCenter.Subscribe<Object>(this, "authenticatedFinished", () =>
  {   
      Navigation.PushAsync(new NavigationPage(new FormsView()));
                        
  });
}

我终于在 MvvmCross 文档中找到了解决方案:https://www.mvvmcross.com/documentation/advanced/customizing-appstart?scroll=100

public class App : MvxApplication
{
    public override void Initialize()
    {       
        RegisterCustomAppStart<AppStart>();
    }
}




    public class AppStart : MvxAppStart
{
    private readonly IAuthenticationService _authenticationService;

    public MvxAppStart(IMvxApplication application, IMvxNavigationService navigationService, IAuthenticationService authenticationService) : base(application, navigationService)
    {
        _authenticationService = authenticationService;
    }

    protected override void NavigateToFirstViewModel(object hint = null)
    {
        try
        {
            // You need to run Task sync otherwise code would continue before completing.
            var tcs = new TaskCompletionSource<bool>();
            Task.Run(async () => tcs.SetResult(await _authenticationService.IsAuthenticated()));
            var isAuthenticated = tcs.Task.Result;

            if (isAuthenticated)
            {
                //You need to Navigate sync so the screen is added to the root before continuing.
                NavigationService.Navigate<HomeViewModel>().GetAwaiter().GetResult();
            }
            else
            {
                NavigationService.Navigate<LoginViewModel>().GetAwaiter().GetResult();
            }
        }
        catch (System.Exception exception)
        {
            throw exception.MvxWrap("Problem navigating to ViewModel {0}", typeof(TViewModel).Name);
        }
    }
}