Android上不全局支持PushAsync,请使用NavigationPage-Xamarin.Forms
在连接到按钮单击事件的Android上不全局支持PushAsync,请使用NavigationPage-Xamarin.Forms,navigation,xamarin,xamarin.android,xamarin.forms,Navigation,Xamarin,Xamarin.android,Xamarin.forms,在连接到按钮单击事件的Xamarin.Forms.ContentPage中,我有以下方法 公共类登录页面:ContentPage { 私有按钮_loginButton=null; 私有条目_PasswordInput=null; 私有条目_UsernameInput=null; 公共登录页() { _UsernameInput=新条目{Placeholder=“Username”}; _PasswordInput=新条目{Placeholder=“Password”,IsPassword=tru
Xamarin.Forms.ContentPage中,我有以下方法
公共类登录页面:ContentPage
{
私有按钮_loginButton=null;
私有条目_PasswordInput=null;
私有条目_UsernameInput=null;
公共登录页()
{
_UsernameInput=新条目{Placeholder=“Username”};
_PasswordInput=新条目{Placeholder=“Password”,IsPassword=true};
_loginButton=新建按钮
{
Text=“登录”,
边界半径=5
}
_loginButton.Clicked+=登录;
内容=新的堆栈布局
{
垂直选项=布局选项。中心,
儿童=
{
_用户名输入、密码输入、登录按钮、,
},
间距=15
};
}
公共异步无效登录(对象发送方、EventArgs和eventsArgs)
{
//你在这里做什么
SSO.MyAuthentication client=新的SSO.MyAuthentication();
bool isAuthenticated=client.Authenticate(_UsernameInput.Text,_PasswordInput.Text);
如果(未经验证)
{
//将主页推到导航堆栈的顶部
Navigation.PushAsync(新主页());
}
}
}
在下面的代码行Navigation.PushAsync(新主页())代码>,调试时出现以下异常:
Android上不全局支持PushAsync,请使用
导航页
如何使用Xamarin.Forms.NavigationPage
对象解决此问题?您需要将登录页面包含在NavigationPage中。这将修复您的错误,但将保留导航堆栈中包含的登录页面
另一种方法是将您的主页设置为应用程序的根目录,然后在其顶部以模式显示登录页面。只有当用户成功登录时,您才会关闭登录页面模式,以便他们可以查看主页。您正在调用“PushAsync”:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btnCourseList_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new PageB());
}
}
MainPage = new NavigationPage(new PageA());
但是您没有启动NavigationPage,这通常是在App.cs类中完成的,或者至少应该在调用“PushAsync”之前启动:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btnCourseList_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new PageB());
}
}
MainPage = new NavigationPage(new PageA());
在app.xaml.cs文件中
替换
MainPage = new <namespace>.MainPage();
我仅使用pushModalAsync更改pushAsync:)
我在混合Rg.Plugins.Popup和ZXin.Net.Mobile Scanner时遇到了一个问题
在弹出窗口中调用扫描仪触发了相同的错误。PushModalAsync解决了错误,但弹出窗口位于扫描上方,因此简单的解决方案是在扫描仪打开之前使弹出窗口不可见
private async void FrmQrCode_Tapped(object sender, EventArgs e)
{
ZXingScannerPage scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
ZXing.BarcodeFormat barcodeFormat = result.BarcodeFormat;
string type = barcodeFormat.ToString();
Device.BeginInvokeOnMainThread(() =>
{
Navigation.PopModalAsync();
this.IsVisible = true;
Token = result.Text.Trim();
});
};
this.IsVisible = false;
await Navigation.PushModalAsync(scanPage);
}
首先在“主应用程序页面”中进行设置,然后在“内容页面”中进行设置以转到其他页面:
确认上一页未使用PushModalAsync。
如果以后使用PushAsync,则会出现错误“Android上全局不支持PushAsync,请使用NavigationPage。”检查在以前的导航中是否使用NavigationPage:
不正确:Application.Current.MainPage=new LoginPage()代码>
正确:Application.Current.MainPage=new-NavigationPage(new-LoginPage())代码>当您添加(在“公共部分类应用程序”中)时:
您可以使用:
等待Navigation.PushAsync(新的bleble()) 在我的App.GetMainPage
方法中,是否将我的LoginPage
包含在NavigationPage
中?另外,我是否可以使用“Navigation.PopAsync()”方法从导航堆栈中删除登录页?是的,您可以将NavPage设置为最外层的页面。我不认为当堆栈上只有一个页面时可以使用PopAsync(),但我没有尝试过。另一种方法是将LoginPage
作为应用程序的主页面,和PushModalAsync
一个新的NavigationPage
,在成功登录时显示应用程序的内容。最好的方法是正常加载应用程序并PushModalAsync()
登录页面。只是要确保防止用户按下Android上的硬件后退按钮public override void OnBackPressed(){if(user.IsAuthenticated(){base.OnBackPressed();}}}
当我试图在模式页面中调用PushAsync时,我也遇到了同样的问题。类似-PushAsync-PushModalAsync-PushAsync的东西(在这里我遇到了问题,您的解决方法不起作用)徒劳地尝试了所有事情,这就像一个符咒(构建Android 6,在LG G2 Android 4.4上模拟)。感谢@Reader Man SanMainPage=new NavigationPage(new PageA());使用应用程序但不使用内容页面有人应该制作一个IDE,将错误警告与投票最高的StackOverflow答案完美结合。感谢您不需要使用new NaigationPage()在PushAsync内部。您可以使用wait-Navigation.PushAsync(新的MainPage2());少一点代码:)wait-Navigation.PushAsync(新的MainPage());无法使用ContentPage我正在获取系统。InvalidOperationException:Android上不全局支持PushAsync,请使用NavigationPage。这会弄乱设计,因为它会在页面顶部添加导航栏。这会弄乱设计,因为它会在页面顶部添加导航栏。
private async void FrmQrCode_Tapped(object sender, EventArgs e)
{
ZXingScannerPage scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
ZXing.BarcodeFormat barcodeFormat = result.BarcodeFormat;
string type = barcodeFormat.ToString();
Device.BeginInvokeOnMainThread(() =>
{
Navigation.PopModalAsync();
this.IsVisible = true;
Token = result.Text.Trim();
});
};
this.IsVisible = false;
await Navigation.PushModalAsync(scanPage);
}
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}