How to use properties from Metrowindow above frame

2019-06-09 04:16发布

问题:

Using Mahapps.metro, and stumbling into a problem.

I want to page paged content, all controlled via a frame within metro window.

There are two things I cannot figure out how to do, which was easy enough whilst working all within one window without the pages.

First off, I want to use their dialogs. The following line worked previousely whilst used directly within the window:

await this.ShowMessageAsync("This is the title", "User Setting: " + x);

However, it now gives the following error:

Instance argument: cannot convert from 'GameWindow.MainMenu' to 'MahApps.Metro.Controls.MetroWindow'

I have tried searching for an answer, as well as trying things such as

await this.Parent.ShowMessageAsync("This is the title", "User Setting: " + x);

but all to no avail.

Secondly, I have a flyout configured in my window, which I need to access from a page within the frame also. I cannot for the life of me figure this one out either...

回答1:

You have to walk up the visual tree to find the MetroWindow instance, e.g.

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

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

TryFindParent<> is an extension method defined in MahApps.Metro.Controls.TreeHelper, and ShowMessageAsync<> is defined in MahApps.Metro.Controls.Dialogs.DialogManager. If you don't want to use extension methods, try this code:

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