如何在列表中显示文本块中的有限文本

时间:2013-12-22 00:34:35

标签: c# arrays windows-phone-8 visual-studio-2013 textblock

将文本内容显示在文本块中。正在从列表中检索数据。反复按按钮可更改列表中文本块的内容。我的代码显示列表中的所有文本,并在显示所有文本时通知用户。

例如,如果列表中有100个项目,则用户的操作将逐一显示100个问题。我想要的是如何设置它来填充我的100个问题中的10个。因此,在显示10个文本后将显示已完成的消息。我还希望在每次更改文本时随机化并随机播放列表。这样每次打开页面时用户都会遇到不同的事情(至少在一段时间内)。 这是我的代码:

public partial class _008Test : PhoneApplicationPage
{
    private List<Question> questionList;
    int currentQuestionIndex = 0;
    private Question currentQuestion;
    int Score = 0;
    public _008Test()
    {
        InitializeComponent();
        InitializeComponent();
        questionList = new List<Question>();
        questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
    questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });

        loadquestion(currentQuestionIndex);
    }
    private void loadquestion(int questionindex)
    {
        currentQuestion = questionList[questionindex];
    }

    private void Next_Click(object sender, System.EventArgs e)
    {
        Score++;
        currentQuestionIndex++;
        if (currentQuestionIndex < questionList.Count)
        {
            loadquestion(currentQuestionIndex);
        }
        else
        {
            MessageBox.Show("You have finished!" + "Score is: " + Score);
        }
    }
}

上面的列表可以包含20个文本。我想让它达到100甚至更多。但我只能展示一切。我将如何显示只有10。因此,必须重新打开页面才能启动另一个文本会话。

2 个答案:

答案 0 :(得分:0)

数到10,随机化索引:

int questionNum = 0;
Random rand = new Random();
int numOfQuestionsInTheGame = 100; // this is the number of all your questions
private void Next_Click(object sender, System.EventArgs e)
{
    Score++;
    if (questionNum < 10)
    {
        loadquestion(rand.Next(0, numOfQuestionsInTheGame));
    }
    else
    {
        MessageBox.Show("You have finished!" + "Score is: " + Score);
    }
    questionNum++;
}

请记住,这可以多次返回相同的索引,因此您可能想要想办法避免这种情况。一种方法可能是保留一个简单的已使用索引列表。

答案 1 :(得分:0)

您可以使用最初填充了问题列表中所有项目的其他临时列表。

然后每次用户按下一个,从临时列表中删除上一个问题,并为currentQuestionIndex分配一个新的随机索引。

如果临时列表为空,则用户已完成您设置的所有问题。

        //if(CorrectAnswer)
          Score++;
        questionNumber++;

        if (currentQuestionIndex < tempList.Count)
        {
            //delete previous item from tempList
            tempList.RemoveAt(currentQuestionIndex);
        }

        //if no more questions, then Display completed and disable Next Button
        if (tempList.Count == 0)
        {
            MessageBox.Show(String.Format("Your have completed , Your Final score is {0}", Score));
            return;
        }

        Random rand = new Random();
        //assign currentQuestionIndex a new random number
        currentQuestionIndex = rand.Next(0, tempList.Count);
        loadquestion(currentQuestionIndex);

每十个问题显示一条消息

questionNumber++;    
if (questionNumber % 10 == 0)
  {
   //show completed messages each time at 10,20,30,.....
  }

这是您使用这些更改修改的代码

 public partial class Page1 : PhoneApplicationPage
{
    private List<Question> questionList;
    private int currentQuestionIndex = 0;
    private Question currentQuestion;
    private int Score = 0;
    public Page1()
    {
        InitializeComponent();
        questionList = new List<Question>();
        questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        tempList = new List<Question>(questionList);

        Random rand = new Random();
        //assign currentQuestionIndex new random number
        currentQuestionIndex = rand.Next(0, tempList.Count);
        loadquestion(currentQuestionIndex);

    }
    private void loadquestion(int questionindex)
    {
        currentQuestion = tempList[questionindex];
    }

    private List<Question> tempList;
    private int questionNumber = 0;
    private void Next_Click(object sender, System.EventArgs e)
    {
        //if(CorrectAnswer)
        Score++;
        questionNumber++;
        if (currentQuestionIndex < tempList.Count)
        {
            //delete previous item from tempList
            tempList.RemoveAt(currentQuestionIndex);
        }

        //if no more questions, then Display completed and disable Next Button
        if (tempList.Count == 0)
        {
            MessageBox.Show(String.Format("Your have completed , Your Final score is {0}", Score));
            return;
        }
        Random rand = new Random();
        //assign currentQuestionIndex a new random number
        currentQuestionIndex = rand.Next(0, tempList.Count);
        loadquestion(currentQuestionIndex);

        if (questionNumber % 10 == 0)
        {
            //show level messages each time at 10,20,.......
            MessageBox.Show(String.Format("Your score is {0}", Score));
        }
    }
}