ListView获取多个条目值以进行验证和发送

时间:2018-09-13 18:27:45

标签: c# xamarin xamarin.forms

希望您能在此方面为我提供帮助。

我有一个来自API的调查,我想在ListView中显示它们,但是我的问题是,如何通过单击“发送”按钮来验证每个条目?

我的代码隐藏

public class SurveyList
{
    public List<QuestionList> Questions { get; set; }
}

public class QuestionList
{
    public string QuestionText { get; set; }
    public string QuestionCode { get; set; }
}

public partial class SurveyPage : ContentPage
{

    public SurveyPage()
    {
        InitializeComponent();

        var surveyListData = new List<QuestionList>
        {
            new QuestionList { QuestionText = "Question 1?", QuestionCode = "01" },
            new QuestionList { QuestionText = "Question 2?", QuestionCode = "02" }
        };

        surveyList.ItemsSource = surveyListData;
    }

    void Handle_Clicked(object sender, System.EventArgs e)
    {
        // Button Clicked Get Values Here and Do Something
    }
}

}

我的XAML

<ListView x:Name="surveyList"
          HasUnevenRows="true"
          SeparatorVisibility="Default" 
          BackgroundColor="White">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="10" BackgroundColor="Purple">
                    <StackLayout Spacing="10" VerticalOptions="Start" HorizontalOptions="FillAndExpand" BackgroundColor="Olive">
                        <Label Text="{Binding QuestionLabel}" TextColor="Navy"/>
                        <Picker x:Name="QuestionPicker">
                            <Picker.Items>
                                <x:String>Yes</x:String>
                                <x:String>No</x:String>
                            </Picker.Items>
                        </Picker>
                    </StackLayout>
                    <StackLayout Spacing="20" VerticalOptions="End" HorizontalOptions="FillAndExpand" BackgroundColor="Maroon">
                        <Button x:Name="surveyButton"
                                Text="Enviar"
                                TextColor="White"
                                BackgroundColor="{StaticResource dpGreen}"
                                Clicked="Handle_Clicked"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

使用此代码,我正在测试如何从单击的按钮中获取所有值

1 个答案:

答案 0 :(得分:1)

使用@Andrew所说的Foreach解决问题。

void Handle_Clicked(object sender, System.EventArgs e)
    {
        foreach(var getValues in survey)
        {
            getValuesOfPickers.Text = getValues.QuestionCode;
        }
    }

不确定是否可以完全解决它,但是正如@Andrew所说,原始问题得到了回答