private void AddNewQuestionsListButton_Click(object sender, RoutedEventArgs e) { string listName = AddNewQuestionsListNameTextBox.Text; if (Regex.IsMatch(listName, @"^[a-zA-Z0-9_]+$")) { questionItems.Clear(); questionsListName = AddNewQuestionsListNameTextBox.Text; UpdateQuestionsListNameTextBox(); loadedQuestionsList = true; UpdateEnabledElements(); AddNewQuestionsListNameTextBox.Text = string.Empty; QuestionManager.ClearQuestions(); QuestionManager.WrongAnsweredQuestions.Clear(); actualAnswersID = 0; editMode = false; loadedQuestionsList = true; questionItems.Clear(); questionsID = questionItems.Count; UpdateEnabledElements(); InitQuestionItems(); ((QuestionsContent)TabManager.GetTab("Questions").Content).Refresh(); var saveFileDialog = new SaveFileDialog { InitialDirectory = @"C:\", Title = $"Save {questionsListName}", CheckPathExists = true, DefaultExt = "json", Filter = "Json files (*.json)|*.json", RestoreDirectory = true }; if (saveFileDialog.ShowDialog() == true) { filePath = saveFileDialog.FileName; SaveQuestions(); QuestionManager.ClearQuestions(); } } else { errorSnackbar.MessageQueue.Enqueue("Name can only contains letters, numbers and underscore.", null, null, null, false, true, TimeSpan.FromSeconds(4)); } }
public static void ReadFile(string fileName, ref Snackbar errorSnackbar) { Mouse.OverrideCursor = Cursors.Wait; QuestionManager.ClearQuestions(); using (var reader = new StreamReader(fileName)) { try { dynamic json = JsonConvert.DeserializeObject(reader.ReadToEnd()); for (int i = 0; i < json.Count; i++) { string question = json[i].Name; var answers = new List <Answer>(); for (int j = 0; j < json[i].Answers.Count; j++) { string answer = json[i].Answers[j].Name; bool right = json[i].Answers[j].Right; answers.Add(new Answer(j, answer, right)); } QuestionManager.AddQuestion(new Question(i, question, answers)); ((QuestionsContent)TabManager.GetTab("Questions").Content).AddNewQuestion(); ((SettingsContent)TabManager.GetTab("Settings").Content).UpdateQuestionItems(fileName); } } catch (Exception) { errorSnackbar.MessageQueue.Enqueue("Can't read file.", null, null, null, false, true, TimeSpan.FromSeconds(4)); } } Mouse.OverrideCursor = Cursors.Arrow; }