如果以 xamarin 形式选择它,如何清除选择器? [英] How to clear picker if It is selected in xamarin forms?

查看:23
本文介绍了如果以 xamarin 形式选择它,如何清除选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择器.第二个选择器取决于第一个选择器.两个选择器都从服务绑定.我正在使用字典对象将数据绑定到选择器.我没有使用 MVVM 模式.

  1. 第一个服务调用,其中绑定了第一个选择器的字典对象.
  2. 然后从该字典对象中填充第一个选择器.那时第二个选择器是空的.
  3. 在第一个选择器调用服务的 selectedIndexChange 事件上绑定第二个选择器的字典对象.
  4. 现在将值从字典对象填充到第二个选择器.(如果已经选择器有数据,则输入 Picker.Items.clear())
  5. 现在,如果我从第二个选择器中选择一些值并更改第一个选择器的值,那么它会在 Picker.Items.clear()
  6. 出现错误

<块引用>

System.ArgumentOutOfRangeException:索引超出范围.必须是非负且小于集合的大小.

参数名称:索引

全局声明:

字典<字符串,字符串>DicObjActivityType;字典<字符串,字符串>DicObjSelfActivity;

First Picker selectedIndexChange 事件

private async void PckrActivityType_SelectedIndexChanged(object sender, EventArgs e){如果(发件人为选择器 == null)返回;别的{var objActivityType = sender as Picker;var Key = DicObjActivityType.FirstOrDefault(x => x.Value == objActivityType.Items[objActivityType.SelectedIndex]).Key;PickedActivityType = 键;如果(键!=0"){PckrSelfActivity.IsEnabled = true;等待 CallGetWebService_SelfActivity(Key);如果(PckrSelfActivity.IsEnabled == true){PckrSelfActivity.Items.Clear();foreach(DicObjSelfActivity.Values 中的字符串项){PckrSelfActivity.Items.Add(items);}}}别的{PckrSelfActivity.IsEnabled = false;}}}

呼叫二选机服务

private async Task CallGetWebService_SelfActivity(string strkey){尝试{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 项目在 response.lstListComboData){DicObjSelfActivity.Add(Items.Value, Items.Text);}}}别的{PckrSelfActivity.IsEnabled = false;}}捕获(例外 e){等待 DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");}}

我参考以下链接来解决这个问题

https://forums.xamarin.com/discussion/55922/picker-clear-system-argumentoutofrangeexception

但没有找到解决方案.

如果选择了任何值,我们无法清除选择器?

我不想使用 BindablePicker 自定义控件.

解决方案

我不确定清除选择器"是否是指重置它,使其不显示任何已选择的项目.但如果这就是您想要的,那么您可以执行以下操作:

  1. 设置 SelectedIndex = -1.这将重置选择器,以便不选择任何项目.

  2. 一旦你设置了 SelectedIndex = -1selectedIndexChange 事件处理程序就会被调用,这会导致

<块引用>

System.ArgumentOutOfRangeException:索引超出范围.必须是非负数且小于集合的大小.

确实是的,SelectedIndex-1,所以它抱怨索引必须是非负的.

  1. 为避免 ArgumentOutOfRangeException,添加一个 if 语句,将您的 SelectedIndexChange 事件处理程序括起来,以便处理程序只执行
    if (SelectedIndexChange !=-1).

总而言之,这样做:

YourPicker.SelectedIndex = -1;YourPicker.SelectedIndexChanged += (sender, e) =>{if (YourPicker.SelectedIndex != -1){//做你的事}}

I have two picker. second picker is dependent on first picker. Both pickers are bind from Service. I am using dictionary object to bind data to picker. I am not using MVVM pattern.

  1. First service call where dictionary object for first picker is bind.
  2. then fill first picker from that dictionary object. At that time Second picker is empty.
  3. On selectedIndexChange event of first picker call service to bind dictionary object of second picker.
  4. Now Fill values to second picker from dictionary object. (If already picker has data then put Picker.Items.clear())
  5. Now If I select some value from second picker and change value of first picker then It gives me error at Picker.Items.clear()

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

Global Declaration :

Dictionary<string, string> DicObjActivityType;

Dictionary<string, string> DicObjSelfActivity;

First Picker selectedIndexChange event

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;
        }
    }
}

Call Service of second picker

private async Task CallGetWebService_SelfActivity(string strkey)
{
    try
    {
        var response = await GetResponseFromWebService.GetResponse<ServiceClasses.RootObject_LstListComboData>(ServiceURL.GetSelfActivity + "ActivityTypeCd=" + strkey);

        if (response.Flag == true)
        {
            DicObjSelfActivity = new Dictionary<string, string>();
            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");
    }
}

I refer following link to solve this issue

https://forums.xamarin.com/discussion/55922/picker-clear-system-argumentoutofrangeexception

but didn't find solution.

We can't clear picker if any of the value is selected?

I don't want to use BindablePicker cutom control.

解决方案

I'm not sure if by "clear the picker" you mean reset it such that it doesn't show any items as selected. But if that's what you want, then you can do the following:

  1. Set SelectedIndex = -1. This resets the picker such that no items are selected.

  2. Once you set SelectedIndex = -1, the selectedIndexChange event handler is called, and that's causing the

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Indeed yes, the SelectedIndex is -1, so it's complaining that the index must be non-negative.

  1. To avoid the ArgumentOutOfRangeException, add an if statement that encloses your SelectedIndexChange event handler so that the handler only executes
    if (SelectedIndexChange != -1).

In summary, do this:

YourPicker.SelectedIndex = -1;
YourPicker.SelectedIndexChanged += (sender, e) =>
{
    if (YourPicker.SelectedIndex != -1)
    {
        //Do your stuff
    }
}

这篇关于如果以 xamarin 形式选择它,如何清除选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆