Alan2
Alan2

Reputation: 24572

How can I call DisplayAlert in a Xamarin Forms application

I have a view model with code like this:

    public async Task StopBtnAsync()
    {
        CancelSpeech();
        App.pauseCard = true;
        if (Settings.mode == MO.Learn)
            StopLearn();
        if (Settings.mode == MO.Practice)
            StopPractice();
        if (Settings.mode == MO.Quiz)
        {
            if (await DisplayAlert("Stop Quiz " + Settings.quiz, "Please confirm", "OK", "Cancel") == true)
                StopQuiz();
        }

    }

Clicking the Stop button tries to bring up a DisplayAlert to confirm.

But DisplayAlert appears in red in the IDE with a message saying it does not exist in the current context.

Note, I tried changing this to: App.Current.MainPage.DisplayAlert but this still does not work. Possibly because this is a shell application.

Can someone advise how I can fix this problem.

Reference:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new AppShell();
    }

Upvotes: 0

Views: 1110

Answers (1)

FreakyAli
FreakyAli

Reputation: 16449

Well DisplayAlert is a method of the Page class you need to do something like this

Application.Current.MainPage.DisplayAlert

Upvotes: 2

Related Questions