问题
I created the simplest WPF project with a MVVM pattern. How can I call ShowMessageAsync
from my viewModel?
if (DisciplineText!="")
{
Disciplines.Add(new Discipline(){ChairID = SelectedChair.ChairID,SemesterID = SelectedSemester.SemesterID,Discipline1 = DisciplineText});
Context.SaveChanges();
}
else
{
// ShowMessageAsync????????
}
回答1:
MetroWindow metroWindow = Application.Current.MainWindow as MetroWindow;
metroWindow.MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Accented; // set the theme
await MetroWindow.ShowMessageAsync("Title", "Message");
You need to use :
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
I hope it help :)
回答2:
One mechanism for handling these have been added since the question was originally asked through what it calls a DialogCoordinator
. This blog post by the author contains a description on how to use it; in case that disappears, the main steps are the following:
Add the following to the xaml of the view:
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"
Inject the DialogCoordinator
into your view-model in whichever way is relevant for your application:
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel(DialogCoordinator.Instance);
}
Start showing some dialogs:
public MainWindowViewModel(IDialogCoordinator dialogCoordinator)
{
this.dialogCoordinator = dialogCoordinator;
}
private IDialogCoordinator dialogCoordinator;
private void ShowDialog()
{
dialogCoordinator.ShowMessageAsync(this, "Title of dialog", "Message in dialog");
}
来源:https://stackoverflow.com/questions/26825250/mahapp-showmessageasync-mvvm