Mahapp ShowMessageAsync MvvM

时间:2014-11-09 05:17:08

标签: c# wpf mvvm mahapps.metro

我使用MVVM模式创建了最简单的WPF项目。如何从我的viewModel中调用ShowMessageAsync

 if (DisciplineText!="")
 {
     Disciplines.Add(new Discipline(){ChairID = SelectedChair.ChairID,SemesterID = SelectedSemester.SemesterID,Discipline1 = DisciplineText});
     Context.SaveChanges();
 }
 else
 {
     //  ShowMessageAsync????????
 }

2 个答案:

答案 0 :(得分:7)

MetroWindow metroWindow = Application.Current.MainWindow as MetroWindow;
metroWindow.MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Accented; // set the theme
await MetroWindow.ShowMessageAsync("Title", "Message");

您需要使用:

using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;

我希望它有所帮助:)

答案 1 :(得分:0)

处理这些问题的一种机制有been added,因为问题原来是通过它所谓的DialogCoordinator来询问的。作者This blog post包含如何使用它的描述;如果消失,主要步骤如下:

将以下内容添加到视图的xaml中:

xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"

以适合您应用的方式将DialogCoordinator注入您的视图模型中:

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel(DialogCoordinator.Instance);
}

开始显示一些对话框:

public MainWindowViewModel(IDialogCoordinator dialogCoordinator)
{
    this.dialogCoordinator = dialogCoordinator;    
}

private IDialogCoordinator dialogCoordinator;

private void ShowDialog()
{
    dialogCoordinator.ShowMessageAsync(this, "Title of dialog", "Message in dialog");
}