【问题标题】:Binding data in windows phonewindows phone 绑定数据
【发布时间】:2014-02-11 11:59:21
【问题描述】:

我是 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;

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

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

请帮我解决这个问题.....

【问题讨论】:

  • 你试过什么?您是否希望我们为您完成整个工作,使用 XAML 设计 UI,以便能够显示多个问题和多个选择答案,然后将它们绑定到来自 Web 服务的数据?不,SO 不是免费的代码服务。您的previous question 中建议的解决方案会发生什么情况?如果您尝试过,请发布您的尝试方式,问题是什么?
  • 感谢您的回复。我尝试在文本块和单选按钮中获取问题和选项,但我只得到第一个问题及其各自的选项。请不要帮助我设计 xaml 和显示选项我只是想要如何做到这一点的概念以及如果你知道请分享..关于我之前的问题,解决方案目前不起作用。
  • 所以你设法得到了第一个问题和显示的各个选项,你是怎么做到的?请张贴它,我们会认为这是你已经尝试过的努力。之后你会期待什么?
  • 我编辑了我的问题,请帮助解决这个问题.....
  • 好,我可以看到你现在如何显示最后一个问题,那么问题/问题是什么?抱歉,如果您认为这很明显,但我不明白。想要更改您的代码以使用数据绑定吗?想要显示所有问题而不是最后一个问题(如何在屏幕上排列所有问题)?希望能够转到下一个/上一个问题?

标签: c# json windows-phone-7


【解决方案1】:

你可以试试这个方法:

声明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 模式执行相同操作,因此请考虑更改标题,因为您当前的代码未使用数据绑定,而此答案也未使用数据绑定。

【讨论】:

  • 感谢您理解我的问题。我从这个概念中得到了答案
猜你喜欢
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多