【问题标题】:Mahapps Metro Dialog without MetroWindow没有 MetroWindow 的 Mahapps Metro 对话框
【发布时间】:2017-03-26 22:06:10
【问题描述】:

我想在我的应用程序中使用 mahapps.metro 的对话框。我的窗口是普通的window

public partial class MainWindow : Window

不是

MetroWindow

在我的按钮方法中我写了这个:

var metroWindow = (Application.Current.MainWindow as MetroWindow);
await metroWindow.ShowMessageAsync("Foo", "Bar");

我在App.xaml.cs 中添加了一个ThemeManager

protected override void OnStartup(StartupEventArgs e)
{
    Tuple<AppTheme, Accent> appStyle = ThemeManager.DetectAppStyle(Application.Current);

    ThemeManager.ChangeAppStyle(Application.Current,
                                ThemeManager.GetAccent("Green"),
                                ThemeManager.GetAppTheme("BaseDark")); // or appStyle.Item1
    base.OnStartup(e);
}

App.xaml里面我加了

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

当我执行我的程序时,我得到一个

NullReferenceException

因为metroWindow == null

我该如何解决这个问题?

【问题讨论】:

  • 你需要找到父母MetroWindow无论如何,所以不清楚为什么不MainWindow
  • MahApps 对话框不能在没有 MetroWindow 的情况下使用,因为窗口样式中有一些东西可以与 Dialogs 一起使用。
  • @punker76 感谢您的评论!

标签: c# wpf xaml mahapps.metro


【解决方案1】:

我的窗口是一个普通的窗口。

那是你的问题。由于ShowMessageAsyncMetroWindow 类的扩展方法,您必须引用MetroWindow 才能调用它。这意味着您必须用MetroWindow 替换您的普通窗口或使用另一个对话框。 ShowMessageAsync 方法仅适用于 MetroWindow

以下代码尝试将应用程序的MainWindow 窗口转换为MetroWindow,但如果主窗口确实是普通窗口,则转换总是会失败:

var metroWindow = (Application.Current.MainWindow as MetroWindow);

这就是你得到NullReferenceException的原因。

我该如何解决这个问题?

您必须使用 MetroWindow。恐怕没有其他解决方案了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多