【问题标题】:Make sure an item is picked with Picker Xamarin Forms确保使用 Picker Xamarin Forms 挑选项目
【发布时间】:2017-10-13 00:41:51
【问题描述】:

我目前正在使用 Xamarin Forms 构建跨平台应用程序,并且我有几个选择器。我需要确保所有的选择器都有一个选定的项目,如果没有,我想将他们的背景设置为红色。 我已经尝试循环到 stacklayout 的每个元素以选择所有 Pickers 并检查他们选择的项目,但它不起作用(似乎我的布局只有 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

<ContentPage 
Title="Diagnostic Habitat"
Padding="20"
xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XXX.DiagnosticHabitatPage">
<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key= "BoutonSauvegarde" TargetType="Button" >
            <Setter Property="BackgroundColor" Value="#6AD0C6"/>
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>

<StackLayout  x:Name="DiagHabitat">
    <ProgressBar Progress="1"/>

    <TableView x:Name ="DiagnosticHabitat" Intent="Form"  HasUnevenRows="True">

        <TableRoot Title="Diagnostic habitat">


            <TableSection Title="Title1">
                <ViewCell>
                    <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
                        <Label VerticalOptions="Center" Text="text1"/>
                        <Picker x:Name="accesPorteEntree" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.AccesPorteEntreeEPC, Mode=OneWayToSource}" >
                            <Picker.Items>
                                <x:String>Seuil haut</x:String>
                                <x:String>Seuil bas</x:String>
                                <x:String>Sans seuil</x:String>
                            </Picker.Items>
                        </Picker>
                    </StackLayout>
                </ViewCell>
                <ViewCell>
                    <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
                        <Label VerticalOptions="Center" Text="text2"/>
                        <Picker x:Name="niveauSecuAcces" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.SecuAccesEPC, Mode=OneWayToSource}">
                            <Picker.Items>
                                <x:String>Bas</x:String>
                                <x:String>Moyen</x:String>
                                <x:String>Haut</x:String>
                            </Picker.Items>
                        </Picker>
                    </StackLayout>
                </ViewCell>

                <ViewCell>
                    <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
                        <Label VerticalOptions="Center" Text="Largeur circulation"/>
                        <Picker x:Name="largeurCirculation" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.LargeurCircuEPC, Mode=OneWayToSource}" >

                            <Picker.Items>
                                <x:String>Inf à 75 cm</x:String>
                                <x:String>75 - 90 cm</x:String>
                                <x:String>Sup à 90 cm</x:String>
                            </Picker.Items>
                        </Picker>
                    </StackLayout>
                </ViewCell>

...

【问题讨论】:

  • DiagHabitat 确实只有 2 个直接子代,一个 ProgressBar 和一个 Table。为什么要明确地按名称检查每个选择器?
  • 感谢您的回答。这只是代码的摘录,我有大约 30 个选择器,所以我正在寻找更优化的解决方案...

标签: xamarin xamarin.forms picker


【解决方案1】:

您可以使用triggers,并使用隐式Style 应用它们。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 谢谢!我使用您的代码并更改了我的 xaml 中的一些元素(基本上删除了 tablesection 并仅使用 stacklayout)并且它工作正常。
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2017-07-14
    相关资源
    最近更新 更多