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

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

问题描述

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

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. 第一次服务调用,其中绑定了第一个选择器的字典对象.
  2. 然后从该字典对象中填充第一个选择器.那时,第二选择器为空.
  3. 在第一个选择器调用服务的selectedIndexChange事件上,以绑定第二个选择器的字典对象.
  4. 现在将值从字典对象填充到第二个选择器. (如果选择器已经有数据,则放置 Picker.Items.clear())
  5. 现在,如果我从第二个选择器中选择一些值并更改第一个选择器的值,那么它会在 Picker.Items.clear()
  6. 处给我错误
  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:索引超出范围.必须是 非负数且小于集合的大小.

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

参数名称:索引

全球宣言:

Dictionary<string, string> DicObjActivityType;

Dictionary<string, string> DicObjSelfActivity;

第一个选择器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<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

但没有找到解决方法.

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

我不想使用BindablePicker定制控件.

推荐答案

我不确定是否通过清除选择器"来重置它,以使其不显示任何选定项.但是,如果您要这样做,则可以执行以下操作:

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. 设置SelectedIndex = -1.这将重置选择器,以便没有选中任何项目.

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

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

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

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

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

的确是,SelectedIndex-1,因此它抱怨索引必须为非负数.

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

  1. 为避免ArgumentOutOfRangeException,请添加一个if语句,该语句包含您的SelectedIndexChange事件处理程序,以便该处理程序仅执行
    if (SelectedIndexChange != -1).
  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天全站免登陆