我正在浏览器中进行用户身份验证。
成功通过身份验证后,浏览器将关闭,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()));
}
}
}发布于 2019-09-17 15:49:15
我终于在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);
}
}
}发布于 2019-09-05 13:00:51
当然,您应该在Forms项目中调用这一行。
您可以使用MessagingCenter
在MainActivity
if (Intent.Data != null)
{
// user authenticated
MessagingCenter.Send<Object>(this, "authenticatedFinished");
}在窗体->the构造函数中的MainPage
public xxxMainPage()
{
//...
MessagingCenter.Subscribe<Object>(this, "authenticatedFinished", () =>
{
Navigation.PushAsync(new NavigationPage(new FormsView()));
});
}https://stackoverflow.com/questions/57805178
复制相似问题