我是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; }
}
现在借助于此,我想在文本块中绑定问题&amp;单选按钮中的选项。 我做以下编码来反序列化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;
这就是我从服务中得到的最后一个问题
我的页面的情景是: - 在提交按钮上单击正确答案将显示&amp;按钮&#34;下一步&#34; 可见以显示下一个问题。
请帮帮我......
答案 0 :(得分:0)
您可以尝试这种方式:
声明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();
“我们如何在”提交“按钮上获得正确的答案&amp; 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模式,因此请考虑更改标题,因为您当前的代码没有使用数据绑定,而且这个答案也没有使用数据绑定。