我正在使用 Xamarin.Forms 和 Xamarin.Auth 构建一个应用程序以使用 Facebook 登录。
这是我正在做的事情:
应用程序.cs:
public App()
{
if (IsAuthenticated)
{
MainPage = new NavigationPage(new DetailPage());
}
else {
MainPage = new NavigationPage(new WelcomePage());
}
}
public Action SuccessfulLoginAction
{
get
{
return new Action(() => MainPage.Navigation.PopModalAsync());
}
}
欢迎页面:
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Project
{
public class WelcomePage : ContentPage
{
public WelcomePage()
{
var login = new Button { Text = "Login" };
login.Clicked += async (sender, e) =>
{
await Navigation.PushModalAsync(new LoginPage());
};
Content = new StackLayout
{
BackgroundColor = Color.FromHex("F0C640"),
Padding = new Thickness(10, 40, 10, 10),
Children = {
new Label { Text = "Welcome", FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.White },
new Label { Text = "Please login", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.White },
login
}
};
}
}
}
登录页面:
using Xamarin.Forms;
using System.Threading.Tasks;
namespace Project
{
public class LoginPage : ContentPage
{
}
}
登录渲染器:
using System;
using Xamarin.Auth;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using Project;
using Project.iOS;
[assembly: ExportRenderer(typeof(LoginPage), typeof(LoginPageRenderer))]
namespace Project.iOS
{
public class LoginPageRenderer : PageRenderer
{
bool IsShown;
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
// Fixed the issue that on iOS 8, the modal wouldn't be popped.
// url : http://stackoverflow.com/questions/24105390/how-to-login-to-facebook-in-xamarin-forms
if (!IsShown)
{
IsShown = true;
var auth = new OAuth2Authenticator(
clientId: App.Instance.OAuthSettings.ClientId, // your OAuth2 client id
scope: App.Instance.OAuthSettings.Scope, // The scopes for the particular API you're accessing. The format for this will vary by API.
authorizeUrl: new Uri(App.Instance.OAuthSettings.AuthorizeUrl), // the auth URL for the service
redirectUrl: new Uri(App.Instance.OAuthSettings.RedirectUrl)); // the redirect URL for the service
auth.Completed += (sender, eventArgs) =>
{
// We presented the UI, so it's up to us to dimiss it on iOS.
DismissViewController (true, null);
if (eventArgs.IsAuthenticated)
{
Console.WriteLine("Authenticated!!!!");
App.Instance.SaveToken(eventArgs.Account.Properties["access_token"]);
App.Instance.SuccessfulLoginAction.Invoke();
}
else {
Console.WriteLine("Not authenticated!!!!");
App.Instance.CanceledLoginAction.Invoke();
}
};
PresentViewController(auth.GetUI(), true, null);
}
}
}
}
一切都很好,我可以显示带有 Facebook 内容和登录的模态页面。身份验证完成后,它会通过 auth.Completed 事件并调用 App.Instance.SuccessfulLoginAction.Invoke(); 但 MainPage.Navigation.PopModalAsync() (在 App.cs 中)不会关闭模态页面。
我猜是模式页面在欢迎页面中打开,我尝试在 App.cs 文件中关闭它。是否有可能它没有引用同一个对象,所以我不能从这里关闭它?当您创建特定于平台的渲染器时,你们如何关闭您的模态页面?
如何关闭此模式页面?
谢谢