[英]Xamarin.Forms PopModalAsync doesn't work
I'm building an application using Xamarin.Forms and Xamarin.Auth to login with Facebook. 我正在使用Xamarin.Forms和Xamarin.Auth构建应用程序以使用Facebook登录。
Here is what i'm doing: 这是我在做什么:
App.cs: App.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());
}
}
WelcomePage: 欢迎页面:
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
}
};
}
}
}
LoginPage: 登录页:
using Xamarin.Forms;
using System.Threading.Tasks;
namespace Project
{
public class LoginPage : ContentPage
{
}
}
LoginRenderer: LoginRenderer:
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);
}
}
}
}
Everything works pretty well, I can display the Modal page with the Facebook content and login. 一切运行良好,我可以显示带有Facebook内容和登录信息的Modal页面。 When the Authentication is done it goes through the auth.Completed event and call the App.Instance.SuccessfulLoginAction.Invoke(); 身份验证完成后,它将通过auth.Completed事件,并调用App.Instance.SuccessfulLoginAction.Invoke();。 but MainPage.Navigation.PopModalAsync() (in the App.cs) does not close the Modal page. 但是MainPage.Navigation.PopModalAsync()(在App.cs中)不会关闭“模式”页面。
What i'm guessing is the Modal page is open in the Welcome Page and I try to close it in the App.cs file. 我猜想是Modal页面在Welcome页面中打开,我尝试在App.cs文件中将其关闭。 Is it possible that it does not refer to the same object so I can't close it from here? 是否有可能没有引用同一个对象,所以我不能从这里关闭它? How do you guys do to close your modal page when you create a platform specific renderer? 创建平台特定的渲染器时,你们如何关闭模态页面?
How can I do to close this Modal Page? 如何关闭此模式页面?
Thanks 谢谢
Although not quite elegant, here is how I handled authentication in my sample for authentication: 尽管不太优雅,这是我在示例中进行身份验证的方式:
public void HandleLoginSucceeded(object sender, EventArgs e)
{
MainPage = new WelcomePage();
}
Since App.cs
is the entry point, I just reset the MainPage to point to a new location. 由于App.cs
是入口点,因此我只是将MainPage重置为指向新位置。 I am not aware of the tradeoffs this might cause; 我不知道这可能导致的权衡; however, I am aware that it works. 但是,我知道它有效。 You can see my sample on Github to see my implementation. 您可以在Github上查看我的示例以查看我的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.