简体   繁体   中英

Binding data in windows phone

I am beginner in Windows Phone.Please help me in this:-

This is the class which I extract from a service:-

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; }

}

Now with the help of this, I want to bind questions in a text block & options in a radio button. I do following coding to deserialize json:-

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

I use this method: wc_DownloadStringCompleted()

and write this code

  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;

this is how i got my last question from service

Scenario of my page is:- on submit button click correct answer will display & a button "Next" is visible to display next question.

Please help me on this.....

You can try this way :

Declare variable of type Question to hold index of current question displayed :

private int CurrentQuestionIndex;

Declare collection variable (use ObservableCollection instead of List if you plan to use data-binding later) to hold all questions from web service :

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

Upon download json string completed set CurrentQuestion to the first question, and store all questions in Questions :

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

DisplayCurrentQuestion();

Refactor your codes to display question into a function ( DisplayCurrentQuestion() ) so we can reuse it for displaying next question :

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;
}

On button "Next" clicked simply change CurrentQuestionIndex to next question and display it :

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

" how we get correctAnswerId & answerId on a "Submit" button ".

You can get it this way :

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
}
....
....

Note that there is another way around to do the same with Data Binding and following MVVM pattern, so consider changing the Title because your current code is not using data binding and this answer is not using data binding too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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