Dillinger
Dillinger

Reputation: 1903

Can't use await on ShowMessageAsync

I'm using Mahapp and I'm trying to wait for the result of the dialog, but the compiler underlined ShowMessageAsync and display me:

ShowMessageAsync doesn't exist in the current context

this is the code:

private async void ShowMessageBox(object sender, RoutedEventArgs e)
{
    var result = await ShowMessageAsync("Hello!", "Welcome to the world of metro!", 
        MahApps.Metro.Controls.MessageDialogStyle.AffirmativeAndNegative);
    if (result == MessageDialogResult.Affirmative)
    {
        this.ShowMessageAsync("Result", "You said: OK");
    }
    else
    {
        this.ShowMessageAsync("Result", "You said: CANCEL");
    }
}

Upvotes: 4

Views: 6025

Answers (3)

Felipe López
Felipe López

Reputation: 51

Example by adding a button with some actions:

XAML

 <Button 
            x:Name="btnAddCustomer"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="10" Width="75"
            Click="ButtonAddCustomer_Click">
        <Button.Content>
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/add.jpg" Height="18"
                       Margin="0 0 5 0"/>
                <TextBlock Text="Add"/>
            </StackPanel>
        </Button.Content>
        
    </Button>

Code Behind:

private async void ButtonAddCustomer_Click(object sender, RoutedEventArgs e)
    {
        var result =await this.ShowMessageAsync("Title", "Felipe", MessageDialogStyle.AffirmativeAndNegative);
        if (result == MessageDialogResult.Affirmative)
        {
            await this.ShowMessageAsync("Result", "You said: OK");
            //^^^^ here
        }
        else
        {
            await this.ShowMessageAsync("Result", "You said: CANCEL");
            //^^^^ here
        }

Upvotes: 1

Amit Hasan
Amit Hasan

Reputation: 1450

Extension method for mahapps async message box.

using System.Windows;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System.Threading.Tasks;
public static class InfoBox
{
    public async static Task<MessageDialogResult> ShowMessageAsync(string title, string Message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null)
    {
         return await ((MetroWindow)(Application.Current.MainWindow)).ShowMessageAsync(title, Message, style, settings);
    }
}

Usage

var res = await InfoBox.ShowMessageAsync(...); 
if (res == MessageDialogResult.Affirmative) 
{ 
     /* Do something*/ 
}

Upvotes: 5

Nasreddine
Nasreddine

Reputation: 37848

You must add the this keyword because the ShowMessageAsync is an extension method and not a member of the MetroWindow class.

var result = await this.ShowMessageAsync("Hello!", ...);
                 //^^^^^ here

You have another error. Instead of:

 MahApps.Metro.Controls.MessageDialogStyle.AffirmativeAndNegative

Use:

MahApps.Metro.Controls.Dialogs.MessageDialogStyle.AffirmativeAndNegative 

And you must add await before these lines:

if (result == MessageDialogResult.Affirmative)
{
    await this.ShowMessageAsync("Result", "You said: OK");
   //^^^^ here
}
else
{
    await this.ShowMessageAsync("Result", "You said: CANCEL");
   //^^^^ here
}

Upvotes: -1

Related Questions