diff --git a/Päevaküsimus/App.xaml b/Päevaküsimus/App.xaml
index fede0cd..b38c17e 100644
--- a/Päevaküsimus/App.xaml
+++ b/Päevaküsimus/App.xaml
@@ -2,7 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Päevaküsimus"
- StartupUri="MainWindow.xaml">
+ StartupUri="MainWindow.xaml"
+ Exit="App_OnExit">
diff --git a/Päevaküsimus/App.xaml.cs b/Päevaküsimus/App.xaml.cs
index ebefc0c..ac9ef64 100644
--- a/Päevaküsimus/App.xaml.cs
+++ b/Päevaküsimus/App.xaml.cs
@@ -13,5 +13,15 @@ namespace Päevaküsimus
///
public partial class App : Application
{
+ public static MainWindowVM mwvm;
+ private void App_OnExit(object sender, ExitEventArgs e)
+ {
+ if (App.mwvm != null)
+ {
+ SerializationService.Serialize(App.mwvm.questions);
+ }
+ }
+
+
}
}
diff --git a/Päevaküsimus/MainWindow.xaml b/Päevaküsimus/MainWindow.xaml
index c266077..1e341a3 100644
--- a/Päevaküsimus/MainWindow.xaml
+++ b/Päevaküsimus/MainWindow.xaml
@@ -19,15 +19,16 @@
-
+
-
+
-
+
+
@@ -42,7 +43,7 @@
-
+
@@ -57,14 +58,25 @@
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
diff --git a/Päevaküsimus/MainWindow.xaml.cs b/Päevaküsimus/MainWindow.xaml.cs
index f4d4671..b844340 100644
--- a/Päevaküsimus/MainWindow.xaml.cs
+++ b/Päevaküsimus/MainWindow.xaml.cs
@@ -31,14 +31,34 @@ namespace Päevaküsimus
private void AllQuestionsListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
- if (this.allQuestionsListBox.SelectedIndex > -1)
+ if (this.allQuestionsListBox.SelectedIndex > 0)
{
_vm.selectedQuestion = allQuestionsListBox.SelectedItem as Question;
}
- else
+ else if (this.allQuestionsListBox.SelectedIndex == 0)
{
- _vm.selectedQuestion = new Question("Uus küsimus");
+ this.allQuestionsListBox.SelectedItem = _vm.CreateNewQuestion();
}
+ this.questionQuestionTextBox.SelectAll();
+ this.questionQuestionTextBox.Focus();
+ }
+
+ private void QuestionQuestionTextBox_OnKeyUp(object sender, KeyEventArgs e)
+ {
+ _vm.selectedQuestion.question = questionQuestionTextBox.Text;
+ }
+
+ private void DisplayNewQuestion_OnClick(object sender, RoutedEventArgs e)
+ {
+ _vm.dayQuestion = _vm.GetQuestionOfTheDay();
+ }
+
+ private void GameAnswerSelectionListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
+ {
+ var answer = gameAnswerSelectionListBox.SelectedItem as Answer;
+ var winText = answer.correct ? "You WON!" : "Vale vastus :(";
+ MessageBox.Show($"Peale sinu on selle vastuse valinud veel {answer.selectionCount} inimest.", winText);
+ answer.selectionCount++;
}
}
}
diff --git a/Päevaküsimus/MainWindowVM.cs b/Päevaküsimus/MainWindowVM.cs
index 305c321..34c4f2d 100644
--- a/Päevaküsimus/MainWindowVM.cs
+++ b/Päevaküsimus/MainWindowVM.cs
@@ -8,7 +8,7 @@ using Päevaküsimus.Models;
namespace Päevaküsimus
{
- class MainWindowVM:NotificationBase
+ public class MainWindowVM : NotificationBase
{
public ObservableCollection questions { get; set; }
private Question _seleceQuestion;
@@ -17,39 +17,59 @@ namespace Päevaküsimus
set
{
_seleceQuestion = value;
- selectedQuestionAnswers = value.answers;
NotifyPropertyChanged();
}
}
- private ObservableCollection _selectedQuestionAnswers;
- public ObservableCollection selectedQuestionAnswers {
- get { return _selectedQuestionAnswers; }
+ private Question _dayQuestion;
+ public Question dayQuestion
+ {
+ get { return _dayQuestion; }
set
{
- _selectedQuestionAnswers = value;
+ _dayQuestion = value;
NotifyPropertyChanged();
}
}
+
private Random _rnd;
public MainWindowVM()
{
this._rnd = new Random();
- this.questions = new ObservableCollection();
- var question = new Question("Mis värvi on armastus?");
- question.AddAnswer(new Answer("Punane", true));
- foreach (var color in new [] {"Roheline", "Sinine", "Roosa", "Lilla"})
+ this.questions = SerializationService.DeSerialize();
+ if (this.questions == null)
{
- question.AddAnswer(new Answer(color));
+ this.questions = new ObservableCollection();
+ var question = new Question("Lisa uus küsimus");
+ this.questions.Add(question);
}
- this.questions.Add(question);
- this.questions.Add(new Question("Test"));
+ App.mwvm = this;
+ }
+
+ public Question CreateNewQuestion()
+ {
+ var question = new Question("Ja minu uueks küsimuseks on?");
+ question.AddAnswer(new Answer("Õige vastus 1", true));
+ foreach (var answer in new[] { "Vale vastus 1", "Vale vastus 2", "Vale vastus 3", "Vale vastus 4" })
+ {
+ question.AddAnswer(new Answer(answer));
+ }
+ this.questions.Insert(1, question);
+ this.selectedQuestion = question;
+ return question;
}
public Question GetQuestionOfTheDay()
{
- return this.questions.ElementAt(this._rnd.Next(0, this.questions.Count));
+ try
+ {
+ return this.questions.ElementAt(this._rnd.Next(1, this.questions.Count));
+ }
+ catch (ArgumentOutOfRangeException e)
+ {
+ return new Question("Palun lisa süsteemi mõni küsimus!");
+ }
}
diff --git a/Päevaküsimus/Models/Answer.cs b/Päevaküsimus/Models/Answer.cs
index 33fc8a8..5925c10 100644
--- a/Päevaküsimus/Models/Answer.cs
+++ b/Päevaküsimus/Models/Answer.cs
@@ -3,11 +3,30 @@ using System.Collections;
namespace Päevaküsimus.Models
{
- public class Answer
+ [Serializable()]
+ public class Answer : NotificationBase, ICloneable
{
- public string answer { get; set; }
+ private string _answer;
+ public string answer {
+ get { return _answer; }
+ set
+ {
+ _answer = value;
+ NotifyPropertyChanged();
+ }
+ }
public bool correct { get; set; }
- public int selectionCount { get; set; }
+ private int _selectionCount;
+
+ public int selectionCount
+ {
+ get { return _selectionCount; }
+ set
+ {
+ _selectionCount = value;
+ NotifyPropertyChanged();
+ }
+ }
public Answer(string answer)
{
@@ -20,5 +39,12 @@ namespace Päevaküsimus.Models
this.answer = answer;
this.correct = correct;
}
+
+ public object Clone()
+ {
+ var c = new Answer(answer.Clone() as string, correct);
+ c.selectionCount = selectionCount;
+ return c;
+ }
}
}
\ No newline at end of file
diff --git a/Päevaküsimus/Models/Question.cs b/Päevaküsimus/Models/Question.cs
index 09f2606..8da4ba6 100644
--- a/Päevaküsimus/Models/Question.cs
+++ b/Päevaküsimus/Models/Question.cs
@@ -4,10 +4,20 @@ using System.Collections.ObjectModel;
namespace Päevaküsimus.Models
{
- public class Question
+ [Serializable()]
+ public class Question : NotificationBase, ICloneable
{
- public string question { get; set; }
+ private string _question;
+ public string question {
+ get { return _question; }
+ set
+ {
+ _question = value;
+ NotifyPropertyChanged();
+ }
+ }
private ObservableCollection _answers;
+ public ObservableCollection answers { get { return _answers; } }
public Question(string question)
{
@@ -15,11 +25,19 @@ namespace Päevaküsimus.Models
this._answers = new ObservableCollection();
}
- public ObservableCollection answers { get { return _answers; } }
-
public void AddAnswer(Answer answer)
{
_answers.Add(answer);
}
+
+ public object Clone()
+ {
+ var q = new Question(_question.Clone() as string);
+ foreach (var answer in _answers)
+ {
+ q.AddAnswer(answer.Clone() as Answer);
+ }
+ return q;
+ }
}
}
\ No newline at end of file
diff --git a/Päevaküsimus/NotificationBase.cs b/Päevaküsimus/NotificationBase.cs
index 9110f84..8eda178 100644
--- a/Päevaküsimus/NotificationBase.cs
+++ b/Päevaküsimus/NotificationBase.cs
@@ -4,6 +4,7 @@ using System.Runtime.CompilerServices;
namespace Päevaküsimus
{
+ [Serializable()]
public class NotificationBase : INotifyPropertyChanged
{
diff --git a/Päevaküsimus/Päevaküsimus.csproj b/Päevaküsimus/Päevaküsimus.csproj
index e0f0fd9..aa2124f 100644
--- a/Päevaküsimus/Päevaküsimus.csproj
+++ b/Päevaküsimus/Päevaküsimus.csproj
@@ -57,6 +57,7 @@
MSBuild:Compile
Designer
+
MSBuild:Compile
Designer
diff --git a/Päevaküsimus/SerializationService.cs b/Päevaküsimus/SerializationService.cs
new file mode 100644
index 0000000..02d4631
--- /dev/null
+++ b/Päevaküsimus/SerializationService.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Collections;
+using System.Collections.ObjectModel;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+using Päevaküsimus.Models;
+
+namespace Päevaküsimus
+{
+ public class SerializationService
+ {
+ static private string _fileName = "data.bin";
+
+ public static void Serialize(object o)
+ {
+ using (Stream stream = new FileStream(_fileName, FileMode.Create, FileAccess.Write, FileShare.Write))
+ {
+ BinaryFormatter binaryFormatter = new BinaryFormatter();
+ binaryFormatter.Serialize(stream, o);
+ }
+ }
+
+ public static ObservableCollection DeSerialize ()
+ {
+ try
+ {
+ using (Stream stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
+ {
+ BinaryFormatter binaryFormatter = new BinaryFormatter();
+ ObservableCollection questions = binaryFormatter.Deserialize(stream) as ObservableCollection;
+ return questions;
+ }
+ }
+ catch (Exception ex)
+ {
+ System.Diagnostics.Trace.WriteLine("Error: " + ex.Message);
+ return null;
+ }
+ }
+ }
+}
\ No newline at end of file