Reputation: 29
I would like to know how to create Messages Dialogs in Mahapps. All the examples i've found were about clicking a button, but i need to create the Message from inside code (in the middle of a Try in a static async i.e.).
So... this is the code behind the MainWindow (From the GIT examples)
public async void ShowMessageDialog(object sender, RoutedEventArgs e)
{
// This demo runs on .Net 4.0, but we're using the Microsoft.Bcl.Async package so we have async/await support
// The package is only used by the demo and not a dependency of the library!
var mySettings = new MetroDialogSettings()
{
AffirmativeButtonText = "Hi",
NegativeButtonText = "Go away!",
FirstAuxiliaryButtonText = "Cancel",
ColorScheme = MetroDialogOptions.ColorScheme
};
MessageDialogResult result = await this.ShowMessageAsync("Hello!", "Welcome to the world of metro!",
MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, mySettings);
if (result != MessageDialogResult.FirstAuxiliary)
await this.ShowMessageAsync("Result", "You said: " + (result == MessageDialogResult.Affirmative ? mySettings.AffirmativeButtonText : mySettings.NegativeButtonText +
Environment.NewLine + Environment.NewLine + "This dialog will follow the Use Accent setting."));
}
How i could i call ShowMessageDialog from any other ViewModels and show the Dialog over that MainWindow?
Thanks!
Upvotes: 0
Views: 1729
Reputation: 1687
You can get the ActiveWindow
Like so :
var metroWindow = Application.Current.Windows.OfType<Window>()
.SingleOrDefault(x => x.IsActive) as MetroWindow;
I would suggest to make a MessageService
that retrive the Active Window and manage all the messages of your application. Furthermore, you should Implement a interface (IMessageService
) if you want the ability to Unit Test your code.
Upvotes: 1