Mahapp ShowMessageAsync MvvM

给你一囗甜甜゛ 提交于 2019-12-11 11:57:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!