我正在为IOS和Android开发Xamarin Forms应用程序。我有3个xamarin表单页面的列表,例如X.xaml,Y.xaml,Z.xaml。页面X.xaml,Y.xaml有1并单击该按钮它打开下一个下一个Xamarin表单。最后Z.xaml有2个按钮btnBack& btnConfirm
<Button x:Name="btnBack"></Button>
<Button x:Name="btnConfirm"></Button>
当我点击后退按钮时,我想将其导航到我的上一页,但我不想像导航后退按钮或硬件后退按钮单击那样。为此,我尝试了这个代码,这是行不通的。在这段代码中,我实现了自定义单选按钮代码,并将相同的代码调用到来自LoadProviderData()
和LoadProviderTimeData()
的不同单选按钮数据,你可以看到代码吧在RadioGroupViewModel.cs
课程中可能会产生问题帮助我。
Z.xaml
代码
<custom:BindableRadioGroup x:Name="MyRadiouGroup" ItemsSource="{Binding
Path=ProviderList.Values}"
SelectedIndex="{Binding SelectedIndex}"
VerticalOptions="CenterAndExpand">
</custom:BindableRadioGroup>
Z.xaml.cs
代码
public partial class Z: ContentPage
{
public Z()
{
InitializeComponent();
this.BindingContext = new RadioGroupViewModel();/
btnBack.Clicked += btnBack_Clicked;
}
private void btnBack_Clicked(object sender, EventArgs e)
{
var existingPages = Navigation.NavigationStack.ToList();
foreach (var page in existingPages)
{
if(page == this)
Navigation.RemovePage(page);
}
}
}
在这段代码中,当我单击Back Button时,它会导航到上一页,即Y.xaml,当我点击Y.xaml上的一个按钮并打开Z.xaml页面时,它会显示空白。
这是我的Y.xaml
代码
<custom:BindableRadioGroup x:Name="MyRadiouGroup" ItemsSource="{Binding Path=ProviderTimeList.Values}"
SelectedIndex="{Binding SelectedIndex}"
VerticalOptions="CenterAndExpand">
</custom:BindableRadioGroup>
Y.xaml.cs
代码
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
this.BindingContext = new RadioGroupViewModel();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
以下是我的RadioGroupViewModel.cs
班级
public class RadioGroupViewModel : BaseViewModel //updated code added
{
public RadioGroupViewModel()
{
providerList = new Dictionary<int, string>();
providerTimeList = new Dictionary<int, string>();
selectedIndex = -1;
LoadProviderData();
LoadProviderTimeData();
}
//Provider Data
private void LoadProviderData()
{
for (int i = 0; i < 100; i++)
{
ProviderList.Add(i, "providerList " + i);
}
}
//Provider Time
private void LoadProviderTimeData()
{
for (int i = 0; i < 50; i++)
{
providerTimeList.Add(i, "ProviderTimeList" + i);
}
}
private Dictionary<int, string> providerTimeList;
public Dictionary<int, string> ProviderTimeList
{
get { return providerTimeList; }
set
{
providerTimeList = value;
OnPropertyChanged(nameof(ProviderTimeList));
}
}
private Dictionary<int, string> providerList;
public Dictionary<int, string> ProviderList
{
get { return providerList; }
set
{
providerList = value;
OnPropertyChanged(nameof(ProviderList));
}
}
}
实际上在Y.xaml
和Z.xaml
页面中有单选按钮。对于单选按钮自定义已实现此库https://github.com/kirtisagar/XamarinFormsRadioButtonXAML/tree/master/XFormsRadioButton并在此类https://github.com/kirtisagar/XamarinFormsRadioButtonXAML/blob/master/XFormsRadioButton/XFormsRadioButton.iOS/Renderer/RadioButtonRenderer.cs中我在第23行获得null ref异常
对于两个Xaml页面,例如Y.xaml & Z.xaml
,我在RadioGroupViewModel.cs
的构建LoadProviderData()
和Y.xaml
中的LoadProviderTimeData()
实施了Z.xaml
类