C# 如何在Xamarin表单中的静态方法中打开页面?
我目前正在使用我的应用程序登录,现在我正在使用iOS 我创建了一个自定义渲染器,如下所示:C# 如何在Xamarin表单中的静态方法中打开页面?,c#,ios,xamarin,xamarin.forms,C#,Ios,Xamarin,Xamarin.forms,我目前正在使用我的应用程序登录,现在我正在使用iOS 我创建了一个自定义渲染器,如下所示: [assembly: ExportRenderer(typeof(TwitterLogin), typeof(TwitterLogin_iOS))] 在iOS渲染器中完成身份验证时: auth.Completed += (object sender, AuthenticatorCompletedEventArgs ee) => { TwitterLogin.SuccessfulLogin
[assembly: ExportRenderer(typeof(TwitterLogin), typeof(TwitterLogin_iOS))]
在iOS渲染器中完成身份验证时:
auth.Completed += (object sender, AuthenticatorCompletedEventArgs ee) =>
{
TwitterLogin.SuccessfulLoginAction.Invoke();
}
我在TwitterLogin页面的共享代码中创建了一个类似这样的函数,该函数在登录完成时运行:
public static Action SuccessfulLoginAction
{
get
{
return new Action(async () =>
{
System.Diagnostics.Debug.WriteLine("login is complete!");
});
}
}
用户登录后,我希望将页面添加到popmodalasync(以前将其推送到当前页面TwitterLogin页面),我尝试在SuccessfullLoginAction操作中添加方法:
await Navigation.PopModalAsync ();
但由于SuccessfulLoginAction是一种静态方法,我无法访问导航
如果我这样做:
auth.Completed += (object sender, AuthenticatorCompletedEventArgs ee) =>
{
TwitterLogin.SuccessfulLoginAction.Invoke();
TwitterLogin log = new TwitterLogin();
log.popPage();
}
并在twitterlogin页面上具有如下功能:
public async void popPage ()
{
await Navigation.PopModalAsync();
}
我发现崩溃索引超出范围。必须是非负数且小于集合的大小,这对我来说很奇怪,因为我从另一个页面开始时使用了以下代码:wait Navigation.PushModalAsync(new TwitterLogin())代码>
我也尝试过使用MessagingCenter
,但由于我无法在静态方法中使用此
,我无法100%确定如何使其工作,当我使用“null”时,我显然会崩溃:
return new Action(async () =>
{
MessagingCenter.Send<TwitterLogin>(null, "popPage");
}
public TwitterLogin()
{
MessagingCenter.Subscribe<TwitterLogin>(null, "popPage", async (sender) => {
await Navigation.PopModalAsync();
});
}
返回新操作(异步()=>
{
MessagingCenter.Send(null,“popPage”);
}
公共TwitterLogin()
{
MessagingCenter.Subscribe(null,“popPage”,异步(发送方)=>{
等待导航。PopModalAsync();
});
}
有没有关于如何调整代码/替代方法以打开当前页面的想法?最后用以下代码解决了这个问题:
TwitterLogin log = new TwitterLogin();
auth.Completed += (object sender, AuthenticatorCompletedEventArgs ee) =>
{
log.OnButtonClicked();
}
在网页上:
public event EventHandler ButtonClicked;
public async void OnButtonClicked()
{
await Application.Current.MainPage.Navigation.PopAsync();
}
为什么不在TwitterLogin.SuccessfullLoginAction.Invoke();
调用之前或之后执行此操作?您的意思是什么?不,我的意思是这不是一个解决方案?将其添加到您在auth.Completed+=
行上执行的调用中。啊,您的意思是将其添加到我的iOS页面的该函数中。当我输入“导航”时但是我找不到它。但是我可以找到NavigationPage,但是如果我继续搜索,之后什么也找不到。这看起来非常像我在处理Xamarin Auth项目时所做的事情。如果你正在尝试类似的事情,我建议你不要看“依赖项服务”路由而不是自定义渲染器。这样做对我来说更有意义。