Я создаю игру в xamarin, в которой я пытаюсь показать всплывающее окно DisplayAlert по окончании игры (через 8 секунд). Однако, как только время достигает нуля, оно не будет отображаться. Если я помещаю DisplayAlert в начале времени, оно работает нормально.
Не могли бы вы объяснить, почему это так?
Строка DisplayAlert находится в методе "OnTimedEvent" в конец кода в этой ViewModel:
using System;
using System.Diagnostics;
using System.Timers;
using CBTGAME1.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CBTGAME1.ViewModels
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GamePageViewModel : ObservableObject
{
Animals aninames = new Animals();
Timer timer;
private int countSecond;
public int CountSecond
{
get { return countSecond; }
set
{
countSecond = value;
OnPropertyChanged(nameof(CountSecond));
}
}
private string nowanimal = null;
public string Nowanimal
{
get { return nowanimal; }
set {
nowanimal = value;
OnPropertyChanged(nameof(Nowanimal));
}
}
public Command Generate { get; }
public Command button1 { get; }
public Command button2 { get; }
public Command button3 { get; }
private int yourscore = 0;
public int Yourscore
{
get { return yourscore; }
set { yourscore = value;
OnPropertyChanged(nameof(Yourscore));
}
}
private string firstb;
private string secondb;
private string thirdb;
public string Firstb
{
get { return firstb; }
set
{
firstb = value;
OnPropertyChanged(nameof(Firstb));
}
}
public string Secondb
{
get { return secondb; }
set
{
secondb = value;
OnPropertyChanged(nameof(Secondb));
}
}
public string Thirdb
{
get { return thirdb; }
set
{
thirdb = value;
OnPropertyChanged(nameof(Thirdb));
}
}
public GamePageViewModel()
{
RndAll();
timer = new Timer();
settimer();
button1 = new Command(() =>
{BtnOperation(firstb); });
button2 = new Command(() =>
{BtnOperation(secondb); });
button3 = new Command(() =>
{BtnOperation(thirdb); });
}
public void BtnOperation(string btname)
{
if (btname == Nowanimal)
Yourscore = Yourscore + 1;
else Yourscore = Yourscore - 1;
RndAll();
}
public void RndAll()
{
Random rnd = new Random();
Nowanimal = aninames.Animalss[rnd.Next(0, 3)];
int i = rnd.Next(0, 3);
Firstb = aninames.Animalss[i];
if (i == 2) i = 0;
else i++;
Secondb = aninames.Animalss[i];
if (i == 2) i = 0;
else i++;
Thirdb = aninames.Animalss[i];
}
public void settimer()
{
CountSecond = 8;
timer.Interval = 1000;
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
CountSecond--;
if (CountSecond == 0)
{
timer.Stop();
App.Current.MainPage.DisplayAlert("finished", "have a good day", "Exit");
}
}
}
}