[英]Mahapps Metro Dialog without MetroWindow
I want to use the dialog's of mahapps.metro in my application. 我想在我的应用程序中使用mahapps.metro对话框。 My window is a normal
window
. 我的窗户是普通
window
。
public partial class MainWindow : Window
not a 不是
MetroWindow
Inside my button method I wrote this: 在我的按钮方法中,我这样写:
var metroWindow = (Application.Current.MainWindow as MetroWindow);
await metroWindow.ShowMessageAsync("Foo", "Bar");
I've added an ThemeManager
inside App.xaml.cs
我在
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);
}
And inside App.xaml
I added 在
App.xaml
我添加了
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
When I execute my programm, I get an 当我执行程序时,我得到一个
NullReferenceException
NullReferenceException
because metroWindow
== null 因为
metroWindow
== null
How can I solve this problem? 我怎么解决这个问题?
My window is a normal window.
我的窗户是普通窗户。
That's your problem. 那是你的问题。 Since
ShowMessageAsync
is an extension method for the MetroWindow
class, you must have a reference to a MetroWindow
in order to be able to call it. 由于
ShowMessageAsync
是MetroWindow
类的扩展方法,因此必须具有对MetroWindow
的引用才能调用它。 This means that you must replace your normal window with a MetroWindow
or use another dialog. 这意味着您必须用
MetroWindow
替换常规窗口或使用另一个对话框。 The ShowMessageAsync
method only works with a MetroWindow
. ShowMessageAsync
方法仅适用于MetroWindow
。
The following code tries to cast the MainWindow
window of your application to a MetroWindow
but the cast will always fail if the main window is indeed a normal window: 以下代码尝试将应用程序的
MainWindow
窗口MetroWindow
转换为MetroWindow
但是如果主窗口确实是普通窗口,则MetroWindow
将始终失败:
var metroWindow = (Application.Current.MainWindow as MetroWindow);
That's why you get a NullReferenceException
. 这就是为什么您得到
NullReferenceException
的原因。
How can I solve this problem?
我怎么解决这个问题?
You must use a MetroWindow. 您必须使用MetroWindow。 There are no other solutions I am afraid.
恐怕没有其他解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.