【问题标题】:how to display limited texts in textblock from a list如何在列表中的文本块中显示有限的文本
【发布时间】:2013-12-22 20:44:50
【问题描述】:

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

例如,如果我的列表中有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。所以必须重新打开页面才能启动另一个文本会话。

【问题讨论】:

  • 它是一个 Windows 手机应用程序。一道银光。不使用 wpf。

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


【解决方案1】:

您可以使用另一个临时列表,该列表最初填充了问题列表中的所有项目。

然后每次用户按下一步,从临时列表中删除上一个问题,并为 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));
        }
    }
}

【讨论】:

    【解决方案2】:

    数到 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++;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 2019-11-24
      • 2020-11-29
      • 2020-11-03
      相关资源
      最近更新 更多