Mahapps消息对话框

时间:2015-07-26 13:25:43

标签: mahapps.metro

我想知道如何在Mahapps中创建消息对话框。我发现的所有例子都是关于单击一个按钮,但是我需要从内部代码创建消息(在静态异步中试试中间即可。)。

所以......这是MainWindow背后的代码(来自GIT示例)

 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."));
    }

我如何从任何其他ViewModel调用ShowMessageDialog并在MainWindow上显示Dialog?

谢谢!

1 个答案:

答案 0 :(得分:1)

你可以得到ActiveWindow像这样:

var metroWindow = Application.Current.Windows.OfType<Window>()
                                     .SingleOrDefault(x => x.IsActive) as MetroWindow;

我建议创建一个MessageService来重新启动Active Window并管理应用程序的所有消息。此外,如果您希望能够对代码进行单元测试,则应该实现一个接口(IMessageService)。