如果在xamarin表格中选择它,如何清除选择器?
我有两个选择器。 第二个选择器取决于第一个选择器。 两个选择器都是从Service绑定的。 我正在使用字典对象将数据绑定到选择器。 我没有使用MVVM模式。
- 第一个服务调用,其中绑定第一个选择器的字典对象。
- 然后从该字典对象中填充第一个选择器。 那时候第二个选择器是空的。
- 在第一个选择器调用服务的selectedIndexChange事件中绑定第二个选择器的字典对象。
- 现在从字典对象中将值填充到第二个选择器。 (如果已经选择器有数据然后放Picker.Items.clear() )
- 现在如果我从第二个选择器中选择一些值并更改第一个选择器的值,那么它会在Picker.Items.clear()中给出错误
System.ArgumentOutOfRangeException:索引超出范围。 必须是非负数且小于集合的大小。
参数名称:index
全球宣言:
Dictionary DicObjActivityType; Dictionary DicObjSelfActivity;
First Picker selectedIndexChange事件
private async void PckrActivityType_SelectedIndexChanged(object sender, EventArgs e) { if (sender as Picker == null) return; else { var objActivityType = sender as Picker; var Key = DicObjActivityType.FirstOrDefault(x => x.Value == objActivityType.Items[objActivityType.SelectedIndex]).Key; PickedActivityType = Key; if (Key != "0") { PckrSelfActivity.IsEnabled = true; await CallGetWebService_SelfActivity(Key); if (PckrSelfActivity.IsEnabled == true) { PckrSelfActivity.Items.Clear(); foreach (string items in DicObjSelfActivity.Values) { PckrSelfActivity.Items.Add(items); } } } else { PckrSelfActivity.IsEnabled = false; } } }
拨打第二个选择器的服务
private async Task CallGetWebService_SelfActivity(string strkey) { try { var response = await GetResponseFromWebService.GetResponse(ServiceURL.GetSelfActivity + "ActivityTypeCd=" + strkey); if (response.Flag == true) { DicObjSelfActivity = new Dictionary(); DicObjSelfActivity.Add("0", "--Select--"); if (response.lstListComboData != null) { foreach (ServiceClasses.LstListComboData Items in response.lstListComboData) { DicObjSelfActivity.Add(Items.Value, Items.Text); } } } else { PckrSelfActivity.IsEnabled = false; } } catch (Exception e) { await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK"); } }
我参考以下链接来解决这个问题
https://forums.xamarin.com/discussion/55922/picker-clear-system-argumentoutofrangeexception
但没有找到解决方案。
如果选择了任何值,我们无法清除选择器?
我不想使用BindablePicker cutom控件。
我不确定是否“清除选择器”意味着重置它以使其不显示任何选定的项目。 但如果这就是你想要的,那么你可以做到以下几点:
-
设置
SelectedIndex = -1
。 这会重置选择器,使得不会选择任何项目。 -
设置
SelectedIndex = -1
,将调用selectedIndexChange
事件处理程序,这会导致
System.ArgumentOutOfRangeException:索引超出范围。 必须是非负数且小于集合的大小。
确实是的, SelectedIndex
是-1
,所以它抱怨索引必须是非负的。
- 要避免ArgumentOutOfRangeException,请添加一个
if
语句,该语句包含SelectedIndexChange
事件处理程序,以便处理程序仅执行
if (SelectedIndexChange != -1)
。
总之,这样做:
YourPicker.SelectedIndex = -1; YourPicker.SelectedIndexChanged += (sender, e) => { if (YourPicker.SelectedIndex != -1) { //Do your stuff } }
我认为您收到此错误是因为您清理了选择器中的所有项目。 我有同样的错误,但我现在使用MVVM,它更简单,你可以更好地管理这种事情。
其实我做了以下工作,它对我来说很好:
//step 1: re-select index to -1 YourPicker.SelectedIndex = -1; //step 2: clear all items YourPicker.Items.Clear(); //refill the picker foreach (var item in subAccountStatus) { YourPicker.Items.Add(item.AccountStatus); } //re-selected index to first item YourPicker.SelectedIndex = 0;
现在Picker
类使用绑定,您可以使用ItemSource
属性将字符串List
绑定到选择器。 我在我的应用程序中修复此问题的方法是创建一个名为GeneratePickerSource()
的辅助方法
这是我在XAML中的Picker
:
然后我的GeneratePickerSource()
方法:
List pickerStrings = new List (); //Add your picker options to the list myPicker.ItemSource = pickerStrings;
通过使用此方法,无论何时调用GeneratePickerSource()
,Picker都将被重置,并且没有选定的项目(对于myPicker
,它将显示“Placeholder”,因为这是它的Title
)。
如果您的列表很短,这种解决方案很有效,这很可能就是这种情况。 但是,如果每次都需要生成一个非常非常大的列表,这可能会很慢。
在您的情况下,您可以像使用List
一样使用Dictionary
执行相同的操作,只需创建一个新的Dictionary
,填充它,并将其设置为ItemSource
。