0

我有一个 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
}

这就是你如何让它在你的应用程序中工作:

Frame rootFrame = Window.Current.Content as Frame;

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

他们在这里所做的是将a分配Frame给'rootFrame'。但是,由于我切换到母版页,我不再有一个Frame而是一个Layout/LayoutAwarePage类型,所以我无法将 CharmFrame 分配给它。我该如何克服这个问题?

任何人?

4

1 回答 1

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");
                }
            }
        }
于 2013-11-12T19:59:26.317 回答