所以我正在尝试使用OAuth和Xamarin.Auth插件实现登录(我正在使用Xamarin.Forms,为ios和android构建)。我成功地跟踪了this tutorial ...一个虚拟的例子。现在我试着去寻找“真正的交易”,但我有点陷入困境:
1)我的应用程序在App.xaml文件中有一个资源字典,似乎没有加载:在App.xaml.cs中,我有
public static Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
_NavPage.Navigation.PushModalAsync(new SampleWithAppResources());
});
}
}
他在自定义渲染器中调用(这里有一个android,但是ios还有另一个): 公共类LoginPageRenderer:PageRenderer { public override void ViewDidAppear(bool animated) { base.ViewDidAppear(动画);
var auth = new OAuth2Authenticator([...]);
auth.Completed += (sender, eventArgs) =>
{
App.SuccessfulLoginAction.Invoke();
if (eventArgs.IsAuthenticated)
{
// The user is authenticated
}
else
{
// The user cancelled
}
};
PresentViewController(auth.GetUI(), true, null);
}
}
SampleWithAppResources不为空,但在此流程中使用时显示为空白页...
2)第二个问题,我的应用程序使用MasterDetailPage。我让代码进化,所以它类似于:
static Root _RootPage;
public App()
{
InitializeComponent();
_RootPage = new Root(){Detail = new NavigationPage(new LoginPage())};
MainPage = _RootPage;
}
public static Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
_RootPage.Detail = new NavigationPage(new SampleWithAppResources());
});
}
}
结果(一旦成功登录):
Unhandled Exception:
Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState occurred
从根本上说,我正在尝试做的事情: - 如果用户未登录,则重定向到LoginPage - 用户登录后显示“HomePage”(此处为SampleWithAppResources)。
顺便说一下,在尝试首先出现LoginPage,然后替换App.Current.MainPage
时,我在Action中出现完全相同的错误如果你们中的任何人都知道如何做到这一点,那将非常有帮助。