绑定数据

问题描述:

我在Windows Phone.Please是初学者帮我在这: -绑定数据

这是我从一个服务提取类: -

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; 

这就是我得到了我的最后一个问题,从服务

我的页面情景是: - on 提交 button c舔正确的答案将显示& a按钮“Next”是可见的显示下一个问题。

请帮我在这.....

+3

你试过了什么?你是否希望我们为你完成整个工作,使用XAML设计UI以便能够显示多个具有多个选择答案的问题,然后将它们绑定到来自Web服务的数据?不,所以SO不是免费的代码服务。并且你的[上一个问题](http://*.com/a/21693618/2998271)中提出的解决方案会发生什么情况?如果你尝试过,发布你如何尝试,以及有什么问题? – har07

+0

感谢您的回复。我尝试在文本块和单选按钮中获得问题和选项,但我只能得到第一个问题及其各自的选项。并且请不要帮助我设计xaml并显示选项我只想要如何做到这一点的概念,如果您知道请分享它..关于我以前的问题,解决方案此时不起作用。 – rr11

+0

所以你设法得到第一个问题和各自的选项显示,你是怎么做到的?请张贴它,我们会认为这是你已经尝试过的努力。你以后期待什么? – har07

你可以试试这个方法: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(); 

我们如何得到正确答案&答案Id在”提交“按钮”。

你可以这样说:

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模式,因此可以考虑,因为当前的代码不使用数据更改标题绑定,这个答案也没有使用数据绑定。

+0

谢谢你理解我的问题。我从这个概念中得到了答案 – rr11