我有一个简单的代码,可以在特定于平台的代码中进行身份验证:
[assembly: ExportRenderer(typeof(FacebookLoginPage), typeof(FacebookLoginRenderer))]
namespace VejoSeriesMobile.Droid.Renderers
{
public class FacebookLoginRenderer : PageRenderer
{
public static string ClientId = "";
public static string ClientSecret = "";
public static string Scope = "email, public_profile";
public static string AuthorizeUrl = "https://m.facebook.com/dialog/oauth";
public static string RedirectUrl = "https://www.facebook.com/connect/login_success.html";
public static string AccessTokenUrl = "https://m.facebook.com/dialog/oauth/token";
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
var activity = this.Context as Activity;
var auth = new OAuth2Authenticator(
clientId: "717427648360004",
scope: Scope,
authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html"));
auth.Completed += OnAuthenticationCompleted;
activity.StartActivity(auth.GetUI(activity));
}
async void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
if (e.IsAuthenticated)
{
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=id,name,picture,email"), null, e.Account);
await request.GetResponseAsync().ContinueWith(t =>
{
var fbUser = JObject.Parse(t.Result.GetResponseText());
var id = fbUser["id"].ToString().Replace("\"", "");
var name = fbUser["name"].ToString().Replace("\"", "");
var email = fbUser["email"].ToString().Replace("\"", "");
var conta = new Account { Username = name };
conta.Properties.Add("email", email);
AccountStore.Create(Context).Save(conta, "VejoSeries");
App.SuccessfulLoginAction.Invoke();
});
}
}
}
}
在我的App便携式类中,我有一个在登录完成后调用的静态方法:
但是在这个范围内我只能使用Static方法,我希望在登录后调用一个非静态方法来重新呈现整个页面(带菜单等)。我怎么能这样做?
在我的成功之上登录行动:
public static Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
//var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
//masterDetailPage.Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(Painel)));
//masterDetailPage.IsPresented = false;
_NavPage.Navigation.PopAsync();
_NavPage.Navigation.PushAsync(new MainPage());
});
}
}
答案 0 :(得分:3)
简短的回答是不要让它变得静止。
如果您将其更改为公共财产,则可以通过App.Current
访问它。
更具体一点:
public Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
//var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
//masterDetailPage.Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(Painel)));
//masterDetailPage.IsPresented = false;
_NavPage.Navigation.PopAsync();
_NavPage.Navigation.PushAsync(new MainPage());
});
}
}
然后从这样的地方打电话:
((App)Application.Current).SuccessfulLoginAction.Invoke();
这样您就可以访问_NavPage
,因为整个事情都绑定到App
的实例。