【问题标题】:Xamarin, Android Activity, IntentFilter, Open Forms ViewXamarin、Android Activity、IntentFilter、打开表单视图
【发布时间】:2020-01-08 08:06:56
【问题描述】:

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

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

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

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

    [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()));
            }
        }
    }

【问题讨论】:

  • 您是否在应用程序的其他位置初始化了 Xamarin.Forms?
  • 我认为 MvxFormsAppCompatActivity 在内部以某种方式做到了这一点。

标签: java android xamarin.forms mvvmcross


【解决方案1】:

当然你应该在 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()));
                        
  });
}

【讨论】:

  • 我正在使用 Mvvscross 导航并尝试了类似的方法,但没有成功:navigationService.Navigate()
【解决方案2】:

我终于在 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多