如何在主页面上使用CharmFlyoutLibrary?

时间:2013-11-12 02:37:20

标签: windows-8 windows-runtime master-pages mvvm-light charms-bar

我有一个Windows 8应用程序,最近我重构它使用'主页'。这意味着有一个“布局”具有一些通用组件,如页眉和页脚。在那个布局中,我有一个框架。每次我想要显示不同的视图时,我都会将其加载到框架中。

这意味着我的启动屏幕不再是Frame类型,而是Layout类型,即LayoutAwarePage。这是我在App.xaml.cs OnLaunched中初始化它的方式:

Layout rootFrame = Window.Current.Content as Layout;

if (rootFrame == null)
{
    rootFrame = new Layout();

问题出现了:我有一个包含一些项目的魅力弹出窗口,例如“设置”。我做了一个漂亮的视图(Flayouts.xaml),其中包含这些弹出窗口的布局。该视图背后的代码如下所示:

public Flyouts()
{
    InitializeComponent();

    SettingsPane.GetForCurrentView().CommandsRequested += Flyouts_CommandsRequested;
}

void Flyouts_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
    // add some commands
}

这就是你在app中使用它的方法:

Frame rootFrame = Window.Current.Content as Frame;

if (rootFrame == null)
{
    rootFrame = new CharmFlyoutLibrary.CharmFrame { CharmContent = new Flyouts() };

他们在这里做的是将Frame分配给'rootFrame'。但是,由于我切换到母版页,因此我不再使用Frame而是Layout / LayoutAwarePage类型,因此我无法为其分配CharmFrame。我该如何克服这个问题?

任何?

1 个答案:

答案 0 :(得分:1)

在框架内导航时,导航到的页面将放置在导航内容属性中。

因此,如果您首先导航到您的布局,那么内容将填充您的页面,您可以导航到您的子页面。我在下面放了一个例子

        Frame rootFrame = Window.Current.Content as Frame;
        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active

        if (rootFrame == null) {
            // Create a Frame to act as the navigation context and navigate to the first page

            rootFrame = new Frame();

            //Associate the frame with a SuspensionManager key                                

            SuspensionManager.RegisterFrame(rootFrame, "AppFrame");

            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) {
                // Restore the saved session state only when appropriate
                try {
                    await SuspensionManager.RestoreAsync();
                } catch (SuspensionManagerException) {
                    //Something went wrong restoring state.
                    //Assume there is no state and continue
                }
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }
        if (rootFrame.Content == null) {
            if (rootFrame.Navigate(typeof(Layout))) {
                var secondFrame = rootFrame.Content as Layout;
                if (!secondFrame.ContentFrame.Navigate(typeof(YourPage)) {

                throw new Exception("Failed to create initial page");
                }
            }
        }