如何在框架页面上方的Metrowindow中使用属性

pin*_*2k4 0 c# wpf mahapps.metro

使用Mahapps.metro,并遇到问题。

我想分页页面内容,所有内容都通过Metro窗口中的框架控制。

我有两件事我不知道该怎么做,这很容易,而在一个没有页面的窗口内全部工作。

首先,我想使用他们的对话框。下面的行以前一直在窗口中直接使用,因此可以正常工作:

await this.ShowMessageAsync("This is the title", "User Setting: " + x);
Run Code Online (Sandbox Code Playgroud)

但是,它现在出现以下错误:

实例参数:无法从“ GameWindow.MainMenu”转换为“ MahApps.Metro.Controls.MetroWindow”

我尝试寻找答案,并尝试诸如

await this.Parent.ShowMessageAsync("This is the title", "User Setting: " + x);
Run Code Online (Sandbox Code Playgroud)

但一切都无济于事。

其次,我在窗口中配置了弹出按钮,也需要从框架内的页面进行访问。我也不可能一辈子都知道这一点...

Tho*_*erg 5

您必须走到视觉树上才能找到MetroWindow实例,例如

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

await this.TryFindParent<MetroWindow>()
    .ShowMessageAsync("This is the title", "User Setting: " + x);
Run Code Online (Sandbox Code Playgroud)

TryFindParent<>是在中定义的扩展方法MahApps.Metro.Controls.TreeHelper,并ShowMessageAsync<>在中定义MahApps.Metro.Controls.Dialogs.DialogManager。如果您不想使用扩展方法,请尝试以下代码:

var window = TreeHelper.TryFindParent<MetroWindow>(this);
await DialogManager.ShowMessageAsync(window, "This is the title", "User Setting: " + x);
Run Code Online (Sandbox Code Playgroud)