Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确保使用Picker Xamarin表单拾取项目_Xamarin_Xamarin.forms_Picker - Fatal编程技术网

确保使用Picker Xamarin表单拾取项目

确保使用Picker Xamarin表单拾取项目,xamarin,xamarin.forms,picker,Xamarin,Xamarin.forms,Picker,我目前正在使用Xamarin表单构建一个跨平台的应用程序,我有几个选择者。我需要确保所有的选取者都有一个选中的项目,如果没有,我想把他们的背景设置为红色。 我已经尝试循环到stacklayout的每个元素,以拾取所有选择器并检查它们所选的项目,但它不起作用。我的布局似乎只有2个子元素,没有选择器。我也看不出如何用行为来做到这一点 我的代码隐藏循环 public void checkChampsVides() { for (int i = 0; i < DiagHa

我目前正在使用Xamarin表单构建一个跨平台的应用程序,我有几个选择者。我需要确保所有的选取者都有一个选中的项目,如果没有,我想把他们的背景设置为红色。 我已经尝试循环到stacklayout的每个元素,以拾取所有选择器并检查它们所选的项目,但它不起作用。我的布局似乎只有2个子元素,没有选择器。我也看不出如何用行为来做到这一点

我的代码隐藏循环

 public void checkChampsVides()
    {
        for (int i = 0; i < DiagHabitat.Children.Count(); i++)
        {
            DisplayAlert("e", DiagHabitat.Children.GetHashCode().ToString(), "ok");
            if (DiagHabitat.Children.ElementAt(i).GetType() == typeof(Picker))
            {

                Picker p = DiagHabitat.Children.ElementAt(i) as Picker;
                if (p.SelectedIndex == 0)
                    p.BackgroundColor = Color.Red;
            }
        }

    }
Xaml


您可以递归地向下遍历包含选择器的最顶层布局容器,并使用一些开关模式匹配来确定何时递归到下一个容器中

public void CheckPickers(Layout layout)
{
    foreach (var child in layout.Children)
    {
        switch (child)
        {
            case Picker picker:
                if (picker.SelectedIndex <= 0)
                    picker.BackgroundColor = Color.Red;
                else
                    picker.BackgroundColor = Color.Green;
                break;
            case Layout l:
                CheckPickers(l);
                break;
            default:
                break;
        }
    }
}
您可以使用,并使用隐式样式应用它们


DiagHabitat只有两个直接子级,一个ProgressBar和一个表。为什么只需按姓名明确检查每个选择器?谢谢您的回答。这只是代码的一个摘录,我有大约30个选择器,所以我正在寻找一个更优化的解决方案…谢谢!我使用您的代码并更改了xaml中的一些元素,基本上删除了tablesection,只使用stacklayout,它工作正常。
public void CheckPickers(Layout layout)
{
    foreach (var child in layout.Children)
    {
        switch (child)
        {
            case Picker picker:
                if (picker.SelectedIndex <= 0)
                    picker.BackgroundColor = Color.Red;
                else
                    picker.BackgroundColor = Color.Green;
                break;
            case Layout l:
                CheckPickers(l);
                break;
            default:
                break;
        }
    }
}
<TableView.Resources>
    <ResourceDictionary>
        <Style TargetType="Picker">
            <Setter Property="BackgroundColor" Value="Green" />
            <Style.Triggers>
                <Trigger TargetType="Picker" Property="SelectedItem" Value="{x:Null}">
                    <Setter Property="BackgroundColor" Value="Red" />
                </Trigger>
                <Trigger TargetType="Picker" Property="SelectedIndex" Value="0">
                    <Setter Property="BackgroundColor" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</TableView.Resources>