Seems to work
This commit is contained in:
parent
077fda732a
commit
f2bb4b3e5c
@ -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">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
|
@ -13,5 +13,15 @@ namespace Päevaküsimus
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -19,15 +19,16 @@
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Label Grid.Column="0" Grid.Row="0" Content="Päevaküsimus on:"/>
|
||||
<Label Grid.Column="1" Grid.Row="0" Content="Placeholder"/>
|
||||
<Label Grid.Column="1" Grid.Row="0" Content="{Binding dayQuestion.question}"/>
|
||||
<Label Grid.Column="0" Grid.Row="1" Content="Vali õige vastuse variant"/>
|
||||
<ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
|
||||
<ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding dayQuestion.answers}" x:Name="gameAnswerSelectionListBox" SelectionChanged="GameAnswerSelectionListBox_OnSelectionChanged">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Label Content="Bla" />
|
||||
<Label Content="{Binding answer}" />
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
<Button x:Name="displayNewQuestion" Content="Kuva uus küsimus" Grid.Column="1" HorizontalAlignment="Right" Margin="0,0,0,0" Grid.Row="1" Padding="7,0" Click="DisplayNewQuestion_OnClick"/>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
<TabItem Header="Admin">
|
||||
@ -42,7 +43,7 @@
|
||||
</Grid.ColumnDefinitions>
|
||||
<Label Grid.Row="0" Grid.Column="0" Content="Küsimused"></Label>
|
||||
<Label Grid.Row="0" Grid.Column="1" Content="Vaata/Muuda"></Label>
|
||||
<ListBox x:Name="allQuestionsListBox" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding questions}" SelectionChanged="AllQuestionsListBox_OnSelectionChanged">
|
||||
<ListBox x:Name="allQuestionsListBox" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding questions}" SelectionChanged="AllQuestionsListBox_OnSelectionChanged" Width="150">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Label Content="{Binding question}" />
|
||||
@ -57,14 +58,25 @@
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"></ColumnDefinition>
|
||||
<ColumnDefinition></ColumnDefinition>
|
||||
<ColumnDefinition Width="Auto"></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Label Grid.Row="0" Grid.Column="0" Content="Küsimus:"></Label>
|
||||
<TextBox Grid.Row="0" Grid.Column="1" Text="Uus Küsimus?"></TextBox>
|
||||
|
||||
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" x:Name="dataGrid" ItemsSource="{Binding selectedQuestion.answers}">
|
||||
|
||||
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding selectedQuestion.question}" KeyUp="QuestionQuestionTextBox_OnKeyUp" x:Name="questionQuestionTextBox"/>
|
||||
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" ItemsSource="{Binding selectedQuestion.answers}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Vastus" IsReadOnly="False" Binding="{Binding answer}" Width="*">
|
||||
</DataGridTextColumn>
|
||||
<DataGridCheckBoxColumn Header="Õige?" IsReadOnly="False" Binding="{Binding correct}">
|
||||
</DataGridCheckBoxColumn>
|
||||
<DataGridTemplateColumn Header="Vastanuid" IsReadOnly="True">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Label Content="{Binding selectionCount}" Margin="0" Padding="0" HorizontalContentAlignment="Center"/>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
|
||||
</Grid>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
|
@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ using Päevaküsimus.Models;
|
||||
|
||||
namespace Päevaküsimus
|
||||
{
|
||||
class MainWindowVM:NotificationBase
|
||||
public class MainWindowVM : NotificationBase
|
||||
{
|
||||
public ObservableCollection<Question> questions { get; set; }
|
||||
private Question _seleceQuestion;
|
||||
@ -17,39 +17,59 @@ namespace Päevaküsimus
|
||||
set
|
||||
{
|
||||
_seleceQuestion = value;
|
||||
selectedQuestionAnswers = value.answers;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<Answer> _selectedQuestionAnswers;
|
||||
public ObservableCollection<Answer> 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<Question>();
|
||||
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<Question>();
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Answer> _answers;
|
||||
public ObservableCollection<Answer> answers { get { return _answers; } }
|
||||
|
||||
public Question(string question)
|
||||
{
|
||||
@ -15,11 +25,19 @@ namespace Päevaküsimus.Models
|
||||
this._answers = new ObservableCollection<Answer>();
|
||||
}
|
||||
|
||||
public ObservableCollection<Answer> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Päevaküsimus
|
||||
{
|
||||
[Serializable()]
|
||||
public class NotificationBase : INotifyPropertyChanged
|
||||
|
||||
{
|
||||
|
@ -57,6 +57,7 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="SerializationService.cs" />
|
||||
<Page Include="MainWindow.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
|
41
Päevaküsimus/SerializationService.cs
Normal file
41
Päevaküsimus/SerializationService.cs
Normal file
@ -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<Question> DeSerialize ()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Stream stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
||||
ObservableCollection<Question> questions = binaryFormatter.Deserialize(stream) as ObservableCollection<Question>;
|
||||
return questions;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Trace.WriteLine("Error: " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user