将不同组的Radiobutton选择存储到列表中

时间:2016-04-06 07:42:52

标签: c# xaml windows-phone-8 windows-phone-8.1

我需要在用户点击Appbar按钮后获取所有选中的单选按钮标签和组名称并将其存储在列表中。

所以我可以将用户提交的答案列表与服务器中的列表进行比较..

如果我使用Checked =" Answer_Checked" ,当我点击问题2中的另一个单选按钮时,列表被覆盖了

public class RootObject
    {
        public RootObject(int id, string question, int qno, int qcount)
        {
            this.id = id;
            this.question = question;
            this.qcount = qcount;
            this.qno = qno;
        }
        public int id { get; set; }
        public string question { get; set; }
        public int qcount { get; set; }
        public int qno { get; set; }
        public int time { get; set; }
    }

public class AnswerObject
    {
        public AnswerObject(int question_id, int answer_id, string answer, int is_right_option)
        {
            this.question_id = question_id;
            this.answer_id = answer_id;
            this.answer = answer;
            this.is_right_option = is_right_option;
        }
        public int question_id { get; set; }
        public int answer_id { get; set; }
        public string answer { get; set; }
        public int is_right_option { get; set; }
    }

    public class Question
    {
        public string QuestionName { get; set; }
        public int qcount { get; set; }
        public int qno { get; set; }

        public ObservableCollection<Option> options { get; set; }
    }

    public class Option
    {
        public string QuestionAnswer { get; set; }
        public string groupname { get; set; }
        public int IsCorrect { get; set; }
    }

C#编码

    var result1 = await response1.Content.ReadAsStringAsync();
            var objResponse1 = JsonConvert.DeserializeObject<List<RootObject>>(result1);

            var result2 = await response2.Content.ReadAsStringAsync();
            var objResponse2 = JsonConvert.DeserializeObject<List<AnswerObject>>(result2);

            for (int i = 0; i < objResponse1.LongCount(); i++)
            {
                ObservableCollection<Option> options1 = new ObservableCollection<Option>();
                for (int j = 0; j < objResponse2.LongCount(); j++)
                {
                    if (objResponse1[i].id == objResponse2[j].question_id)
                    {
                        options1.Add(new Option() { QuestionAnswer = objResponse2[j].answer, IsCorrect = objResponse2[j].is_right_option, groupname = objResponse2[j].question_id.ToString() });
                    }
                }
                questions.Add(new Question() { QuestionName = objResponse1[i].question, qno=i + 1, qcount =objResponse1.Count, options = options1 });
            }
            flipView.ItemsSource = questions;

XAML编码

<FlipView x:Name="flipView" HorizontalAlignment="Left" VerticalAlignment="Top"  ItemsSource="{Binding}" Margin="0,35,0,0">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <ListView Name="ItemData" SelectionMode="None" ItemsSource="{Binding}" >
                    <Grid x:Name="ContentPanel">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock x:Name="Testing" Margin="10,20" FontSize="22.333">
                        <Run Text="{Binding qno}"/>
                        <Run Text="of"/>
                        <Run Text="{Binding qcount}"/>
                        </TextBlock>
                        <TextBlock x:Name="Question"   Text="{Binding QuestionName}" Margin="10,60" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="22.333" TextWrapping="Wrap"/>
                        <ListBox Grid.Row="1" Padding="0" Margin="10,-10"  ItemsSource="{Binding options}" Background="Transparent">

                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <RadioButton x:Name="Answer" GroupName="{Binding groupname}" Checked="Answer_Checked" Tag="{Binding IsCorrect}" Margin="10,2">
                                        <RadioButton.Content>
                                            <TextBlock Text="{Binding QuestionAnswer}" Foreground="White"/>
                                        </RadioButton.Content>
                                    </RadioButton>
                                                                        </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </ListView>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

以JSON格式回答

[{"question_id":3,"answer_id":1,"answer":"10%","is_right_option":0},{"question_id":3,"answer_id":2,"answer":"10.25%","is_right_option":1},{"question_id":3,"answer_id":3,"answer":"10.5%","is_right_option":0},{"question_id":3,"answer_id":4,"answer":"None of these","is_right_option":0},{"question_id":4,"answer_id":5,"answer":"Rs. 2.04","is_right_option":1},{"question_id":4,"answer_id":6,"answer":"Rs. 3.06","is_right_option":0},{"question_id":4,"answer_id":7,"answer":"Rs. 4.80","is_right_option":0},{"question_id":4,"answer_id":8,"answer":"Rs. 8.30","is_right_option":0}]

JSON格式的问题

 [{"id":3,"question":"An automobile financier claims to be lending money at simple interest, but he includes the interest every six months for calculating the principal. If he is charging an interest of 10%, the effective rate of interest becomes: ","time":1},{"id":4,"question":"What is the difference between the compound interests on Rs. 5000 for 1 years at 4% per annum compounded yearly and half-yearly? ","time":1}]

1 个答案:

答案 0 :(得分:0)

我使用Dictionary而不是使用List

解决了它
Animal