Mógłby ktoś pomóc mi i pokazać przykład polegający na tym, by np.: pola z tabeli "Pytania" tj. "pytanie"|"odpowiedź A"|"odpowiedź D"|"odpowiedź C"|"odpowiedź D"|prawidłowa_odpowiedź" były przekazywane odpowiednio do textblocka i radiobuttonów?
Rozwiązanie łopatologiczne, ale niezbyt piękne:
Klasa pytań:
public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public string Answer1 { get; set; }
public string Answer2 { get; set; }
public string Answer3 { get; set; }
public string Answer4 { get; set; }
public int CorrectAnswer { get; set; }
}
XAML głównego widoku:
<StackPanel x:Name="spQuestion">
<TextBlock x:Name="tbQuestionText" />
<RadioButton x:Name="rbAnswer1" GroupName="answers" />
<RadioButton x:Name="rbAnswer2" GroupName="answers" />
<RadioButton x:Name="rbAnswer3" GroupName="answers" />
<RadioButton x:Name="rbAnswer4" GroupName="answers" />
<Button Content="Następne pytanie" Click="Button_Click" />
</StackPanel>
Cała logika (tworzenie bazy pytań i odpowiedzi, jeśli nie ma, ładowanie losowej listy pytań, przechodzenie pomiędzy pytaniami):
public sealed partial class MainPage : Page
{
SQLiteConnection dbconn;
IList<Question> questions = null;
int current = 0;
public MainPage()
{
InitializeComponent();
var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, "database.db");
if (!File.Exists(path))
{
// jeżeli nie ma pliku bazy w folderze aplikacji, stwórz bazę pytań
dbconn = new SQLiteConnection(path);
dbconn.CreateTable<Question>();
dbconn.Insert(new Question { Id = 1, Text = "W którym roku był chrzest Polski?", Answer1 = "966", Answer2 = "1410", Answer3 = "2005", Answer4 = "Jeszcze nie nastąpił", CorrectAnswer = 1 });
dbconn.Insert(new Question { Id = 2, Text = "Kim był Mieszko I?", Answer1 = "Królem", Answer2 = "Księciem", Answer3 = "Wodzem", Answer4 = "Biskupem", CorrectAnswer = 2 });
dbconn.Commit();
}
else
{
dbconn = new SQLiteConnection(path);
}
// załaduj pytania w losowej kolejności
questions = dbconn.Query<Question>("SELECT * FROM Question ORDER BY RANDOM()");
current = -1;
LoadNextQuestion();
}
private async void LoadNextQuestion()
{
current++;
if (current >= questions.Count)
{
MessageDialog md = new MessageDialog("Koniec gry!");
await md.ShowAsync();
return;
}
var currentQuestion = questions[current];
tbQuestionText.Text = currentQuestion.Text;
rbAnswer1.Content = currentQuestion.Answer1;
rbAnswer1.IsChecked = false;
rbAnswer2.Content = currentQuestion.Answer2;
rbAnswer2.IsChecked = false;
rbAnswer3.Content = currentQuestion.Answer3;
rbAnswer3.IsChecked = false;
rbAnswer4.Content = currentQuestion.Answer4;
rbAnswer4.IsChecked = false;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
// sprawdzanie czy rbAnswer[numerpoprawnejodpowiedzi] jest zaznaczony - jeśli tak, ładuj kolejne pytanie
if (spQuestion.Children.OfType<RadioButton>().First(x => x.Name == "rbAnswer" + questions[current].CorrectAnswer).IsChecked.Value)
{
LoadNextQuestion();
}
else
{
MessageDialog md = new MessageDialog("Zła odpowiedź!");
await md.ShowAsync();
}
}
}