首页 > 解决方案 > 如何在一个函数中使用列表,该函数在另一个函数中添加了一些数据

问题描述

所以这是我的第一个文件 QuizzyService:

    List<Question> newQuestions = new List<Question>();
    public static List<Question> QuizzyServiceQuestions()
    {
        using (Stream stream = File.Open("..\\Debug\\questions.bin", FileMode.Open))
        {
            var binaryFormatter = new BinaryFormatter();
            return (List<Question>)binaryFormatter.Deserialize(stream);
        }
    }

    int curQuestion = 0;

    
    public Question NewGame(int questionCount) 
    {
        Random r = new Random();
        for(int i = 0; i < questionCount; i++)
        {
            Question x = QuizzyServiceQuestions()[r.Next(0, QuizzyServiceQuestions().Count)];
            while (newQuestions.Contains(x))
            {
                x = QuizzyServiceQuestions()[r.Next(0, QuizzyServiceQuestions().Count)];
            }
            newQuestions.Add(x);
        }
        Console.WriteLine(newQuestions.Count);
        return newQuestions[0];
    }

    public int CheckAnswer(int questionId, int answerId)
    {
        List<int> IdList = new List<int>();
        for (int i = 0; i < QuizzyServiceQuestions().Count; i++)
        {
            IdList.Add(QuizzyServiceQuestions()[i].Id);
        }
        return IdList.Single(i => i == questionId);
    }

    public Question GetNextQuestion()
    {
        curQuestion += 1;
        Console.WriteLine(newQuestions.Count);
        //Console.WriteLine(curQuestion);
        //Console.WriteLine(this.newQuestions.Count);
        return newQuestions[curQuestion];
    }

这里是 xaml.cs 文件的相关部分:

    private int totalQuestionCount;
    private int currentQuestionCount = 1;
    private int correctAnswerCount;
    Question newQuestion = new Question();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnNewGame_Click(object sender, RoutedEventArgs e)
    {
        if (!int.TryParse(tbQuestionCount.Text.Trim(), out totalQuestionCount))
        {
            tblStatus.Text = "Invalid question count!";
            return;
        }

        correctAnswerCount = 0;
        tblStatus.Text = string.Empty;


        using(WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService"))
        {
            newQuestion = client.NewGame(Convert.ToInt32(totalQuestionCount));
            UpdateUserInterface(newQuestion);
        }


    }

    private void btnAnswer_Click(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;
        int answerId = -1;
        switch (button.Name)
        {
            case "btnAnswerA": answerId = 0; break;
            case "btnAnswerB": answerId = 1; break;
            case "btnAnswerC": answerId = 2; break;
            case "btnAnswerD": answerId = 3; break;
            default:
                // This should never happen
                MessageBox.Show("Invalid button name detected - contact support!");
                return;
        }



        using (WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService"))
        {
            if (answerId == client.CheckAnswer(newQuestion.Id, answerId))
            {
                tblStatus.Text = string.Empty;
                tblStatus.Text = "Correct! Total: " + currentQuestionCount + " / " + totalQuestionCount;
                currentQuestionCount += 1;
                UpdateUserInterface(client.GetNextQuestion());
            }
            else
            {

                tblStatus.Text = string.Empty;
                UpdateUserInterface(client.GetNextQuestion());
            }
        }

我的问题是,当我触发 btnAnswer_Click 时,它没有在 NewGame 中创建的列表,但我不允许执行 NextQuestion(Question questionList) 之类的操作

如果您需要更多信息,请告诉我,我会确保尽快提供更多信息!

标签: c#wpflistfunctionwcf

解决方案


我找到了解决方法......我只需要在 MainWindow() 之前删除并添加“WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService")”


推荐阅读