Xamarin.Auth正在重新初始化应用程序
我只在UWP上遇到这个问题Xamarin.Auth正在重新初始化应用程序,xamarin,xamarin.uwp,xamarin.auth,Xamarin,Xamarin.uwp,Xamarin.auth,我只在UWP上遇到这个问题 private void Button_OnClicked(object sender, EventArgs e) { var clientId = Constants.GoogleUWPClientID; var clientSecret = Constants.GoogleUWPClientSecret; var redirectUrl = Constants.GoogleU
private void Button_OnClicked(object sender, EventArgs e)
{
var clientId = Constants.GoogleUWPClientID;
var clientSecret = Constants.GoogleUWPClientSecret;
var redirectUrl = Constants.GoogleUWPRedirectUrl;
var auth = new OAuth2Authenticator(
clientId: clientId,
clientSecret: clientSecret,
scope: Constants.GoogleScope,
authorizeUrl: new Uri(Constants.GoogleAuthorizeUrl),
accessTokenUrl: new Uri(Constants.GoogleAccessTokenUrl),
redirectUrl: new Uri(redirectUrl),
getUsernameAsync: null,
isUsingNativeUI: true
);
// Login Events
auth.Completed += AuthOnCompleted;
auth.Error += AuthOnError;
auth.IsLoadableRedirectUri = true;
AuthenticationState.Authenticator = auth;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(auth);
}
我确实在UWP上初始化了Xamarin.Auth
Xamarin.Forms.Forms.Init(e);
global::Xamarin.Auth.Presenters.UWP.AuthenticationConfiguration.Init();
这是CompletedEvent处理程序
private async void AuthOnCompleted(object sender, AuthenticatorCompletedEventArgs authenticatorCompletedEventArgs)
{
//var auth = sender as Xamarin.Auth.OAuth2Authenticator;
//auth.DoNotEscapeScope = true;
// We presented the UI, so it's up to us to dimiss it on iOS.
//DismissViewController(true, null);
if (authenticatorCompletedEventArgs.IsAuthenticated)
{
var request = new OAuth2Request("GET", new Uri(Constants.GoogleUserInfoUrl), null, authenticatorCompletedEventArgs.Account);
var response = await request.GetResponseAsync();
if (response != null)
{
var userJson = response.GetResponseText();
var user = JsonConvert.DeserializeObject<GoogleUser>(userJson);
//UserPicture = user.Picture;
//GivenName = user.GivenName;
//Email = user.Email;
}
}
else
{
// The user cancelled
//ErrorMessage = "Cancelled authentication !";
}
}
我已经测试了您的代码并复制了“问题”。验证完成后,应用程序将刷新是正常行为。因为当显示登录页面时,将替换当前内容页面
public void Login(Authenticator authenticator)
{
authenticator.Completed += AuthenticatorCompleted;
System.Type page_type = authenticator.GetUI();
Windows.UI.Xaml.Controls.Frame root_frame = null;
root_frame = Windows.UI.Xaml.Window.Current.Content as Windows.UI.Xaml.Controls.Frame;
root_frame.Navigate(page_type, authenticator);
return;
}
当AuthOnCompleted
时,便携库将被重新加载
public MainPage()
{
this.InitializeComponent();
LoadApplication(new XamarinAuthTest.App());
}
因此,应用程序将“重新启动/刷新”
编辑01
如果
NavigationCacheMode=“Enabled”
,则在完成身份验证后,应用程序将不会重新启动。因为MainPage
的构造函数方法不会在缓存模式下执行。如果应用程序可以接受页面缓存设计模式。你不必担心应用程序的性能。我已经测试了你的代码并重现了“问题”。验证完成后,应用程序将刷新是正常行为。因为当显示登录页面时,将替换当前内容页面
public void Login(Authenticator authenticator)
{
authenticator.Completed += AuthenticatorCompleted;
System.Type page_type = authenticator.GetUI();
Windows.UI.Xaml.Controls.Frame root_frame = null;
root_frame = Windows.UI.Xaml.Window.Current.Content as Windows.UI.Xaml.Controls.Frame;
root_frame.Navigate(page_type, authenticator);
return;
}
当AuthOnCompleted
时,便携库将被重新加载
public MainPage()
{
this.InitializeComponent();
LoadApplication(new XamarinAuthTest.App());
}
因此,应用程序将“重新启动/刷新”
编辑01
如果NavigationCacheMode=“Enabled”
,则在完成身份验证后,应用程序将不会重新启动。因为MainPage
的构造函数方法不会在缓存模式下执行。如果应用程序可以接受页面缓存设计模式。你不必担心应用程序的性能