Xamarin 按钮x:Name=“btnBack”未导航到上一页
我正在为IOS和Android开发Xamarin表单应用程序。我有3个Xamarin表单页面的列表,比如X.xaml、Y.xaml、Z.xaml。页面X.xaml,Y.xaml有1个,点击该按钮,打开下一个Xamarin表单。最后在Z.xaml上有两个按钮btnBack和btnConfirmXamarin 按钮x:Name=“btnBack”未导航到上一页,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在为IOS和Android开发Xamarin表单应用程序。我有3个Xamarin表单页面的列表,比如X.xaml、Y.xaml、Z.xaml。页面X.xaml,Y.xaml有1个,点击该按钮,打开下一个Xamarin表单。最后在Z.xaml上有两个按钮btnBack和btnConfirm public partial class Y: ContentPage { public Y() { InitializeCompon
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
在这段代码中,当我单击Back按钮时,它会将我导航到prevoius页面ie Y.xaml。一旦我单击Y.xaml上的按钮并打开Z.xaml页面,它就会显示为空白
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
Y.xaml.cs
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
您应该调用Navigation.popsync返回@Kowalski得到了正确的答案,但它是不完整的
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
您必须等待Navigation.popsync,否则可能会弄乱导航堆栈。所以,永远等待它。例如:
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
async void btnBack_Clicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
除此之外,请注意Xamarin.Forms中与导航相关的一些bug,这里有一个可能也是您感兴趣的:
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
注意:在这里您可以在上找到官方指南。我在2-3次单击btnBack_单击和btnZ_单击后尝试了PopAsync,然后Z.xaml页面将不再为空,确认按钮将不显示。给我错误System.NullReferenceException:对象引用未设置为具有相同问题的对象的实例给我错误System.NullReferenceException:对象引用未设置为对象的实例调试代码并共享堆栈跟踪。异常在哪里抛出?很可能与导航无关。我建议您复习一下您的问题,因为原始问题与当前问题不同。您的代码示例不完整,说明有误导性。
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}