繁体   English   中英

在Windows Phone中绑定数据

[英]Binding data in windows phone

我是Windows Phone的初学者。请在以下方面帮助我:-

这是我从服务中提取的类:-

public class Answer
{
    public string answerId { get; set; }
    public string answer { get; set; }
}

public class Question
{
    public string questionId { get; set; }
    public string questionTitle { get; set; }
    public string storyUrl { get; set; }
    public string correctAnswerId { get; set; }
    public List<Answer> answers { get; set; }
}

public class RootObject
{
    public string response { get; set; }
    public string message { get; set; }
    public string questionType { get; set; }
    public string device_id { get; set; }
    public string quiz_type { get; set; }
    public int totalQuestion { get; set; }
    public List<Question> questions { get; set; }

}

现在借助此功能,我想将问题绑定到文本框和单选按钮中的选项中。 我执行以下编码来反序列化json:-

WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("my service url"));

我使用这种方法:wc_DownloadStringCompleted()

并编写这段代码

  var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);

        val = rootObject.device_id;

        Question ques = new Question
        {
            questionTitle = rootObject.questions.Last().questionTitle,
            answers = rootObject.questions.Last().answers.Select(ans => new Answer { answer = ans.answer, answerId = ans.answerId }).ToList(),
            questionId = rootObject.questions.Last().questionId,
            storyUrl = rootObject.questions.Last().storyUrl,
            correctAnswerId = rootObject.questions.Last().correctAnswerId

        };
        txtQuestion.DataContext = ques.questionTitle;
        rb1.Content = ques.answers.ElementAt(0).answer;
        rb2.Content = ques.answers.ElementAt(1).answer;
        rb3.Content = ques.answers.ElementAt(2).answer;
        rb4.Content = ques.answers.ElementAt(3).answer;

这就是我如何从服务中得到我的最后一个问题

我的页面的场景是:-在“ 提交”按钮上,单击正确答案将显示&可见“下一步”按钮以显示下一个问题。

请帮我这个.....

您可以这样尝试:

声明类型为Question变量以保存显示的当前问题的索引:

private int CurrentQuestionIndex;

声明集合变量(如果打算以后使用数据绑定,请使用ObservableCollection而不是List)来保存来自Web服务的所有问题:

public List<Question> Questions { get; set; }

下载完成json字符串后,将CurrentQuestion设置为第一个问题,并将所有问题存储在Questions

var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
val = rootObject.device_id;
//store all questions
Questions = rootObject.questions;

DisplayCurrentQuestion();

重构代码以将问题显示到函数中( DisplayCurrentQuestion() ),以便我们可以重用它来显示下一个问题:

private void DisplayCurrentQuestion()
{
    Question ques = Questions[CurrentQuestionIndex];
    txtQuestion.Text = ques.questionTitle;
    rb1.Content = ques.answers[0]answer;
    rb2.Content = ques.answers[1].answer;
    rb3.Content = ques.answers[2].answer;
    rb4.Content = ques.answers[3].answer;
}

单击“下一步”按钮,只需将CurrentQuestionIndex更改为下一个问题并显示它:

CurrentQuestionIndex++;
if(CurrentQuestionIndex > Questions.Count)
{
    //this is the last question, no next question available
}
else DisplayCurrentQuestion();

我们如何在“提交”按钮上获取正确的AnswerId和answerId ”。

您可以通过以下方式获得它:

Question ques = Questions[CurrentQuestionIndex];
var correctAnswerId = ques.correctAnswerId;

if(rb1.IsChecked && ques.answers(0).answerId == correctAnswerId)
{
    //selected answer is correct answer
}
//else check next radio button
else if(rb2.IsChecked && ques.answers(1).answerId == correctAnswerId)
{
    //selected answer is correct answer
}
....
....

请注意,还有另一种方法可以对数据绑定和以下MVVM模式进行操作,因此请考虑更改标题,因为您当前的代码未使用数据绑定,并且此答案也未使用数据绑定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM