【问题标题】:How to change the default frame of the App to a "sub frame"?如何将 App 的默认框架更改为“子框架”?
【发布时间】:2015-11-29 14:38:27
【问题描述】:

我急需帮助。使用 SplitView 处理 WinRT 应用程序 (Windows 10)。使用 MVVMLight(而不是 Template10),因为我已经在标准 MVVM 中做了很多工作来改变它。我在一个名为 Shell 的单独类中添加了 SplitView,这就是我在许多不同教程中看到它的方式。

您可能已经知道,SplitView 基本上在应用程序中有一个永久的侧边栏/窗格,而内容页面在较小的框架内加载。我似乎无法让我已经存在的视图加载到 SplitView 的较小框架中。在我的 Shell.xaml 中:

  <Grid>
    <SplitView x:Name="ShellSplitView" DisplayMode="CompactOverlay"  IsPaneOpen="{Binding PaneOpen}" 
            CompactPaneLength="40" OpenPaneLength="150">
        <SplitView.Pane>
            <Grid x:Name="SplitViewPane">
          ...
            </Grid>
        </SplitView.Pane>
        <SplitView.Content>
            <Frame x:Name="ShellViewFrame">                    
            </Frame>
        </SplitView.Content>
    </SplitView>
</Grid> 

我试图在 SpliteView.Content 中定义的框架内显示我的所有视图,但遇到了问题。我在视图中导航,我使用的是 MVVM-Light 附带的 NavigationService 的 MVVM 方法。在我的 App.xaml.cs 我有:

        Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame == null)
        {
            rootFrame = new Frame();
            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            Window.Current.Content = new Shell(rootFrame);    
        } 

        if (rootFrame.Content == null)
        {
            rootFrame.Navigate(typeof(Shell), e.Arguments);
        }

MainPage 甚至不会显示在 ShellViewFrame 中,除非我在 Shell 类代码隐藏中导航到它。

所以,我的问题是:是否可以将 ShellViewFrame 设为 NavigationService 的默认框架?或者,我可以在整个应用程序中获取对 ShellViewFrame 的引用,以便所有视图都加载到较小的框架内吗?或者有没有完全不同的方法来做到这一点?

【问题讨论】:

  • MVVMLight 中有一个 INavigationService 接口,您可以使用它来编写导航服务以在框架中而不是整个页面中导航。
  • 还有一个接受对象参数的 NavigateTo 重载。您可以加载整个页面并使用参数告诉主页在 ShellViewFrame 中加载哪个页面

标签: c# mvvm windows-runtime mvvm-light


【解决方案1】:

如果您只想在SplitView.Content 中加载视图,例如,如果您的SpiltView 在您的MainPage 中,您应该使用:

ShellViewFrame.Navigate(typeof(...));

因此,如果您想显示SpiltView 及其ShellViewFrame,您可以在App.xaml.cs 中继续加载MainPage

rootFrame.Navigate(typeof(MainPage), e.Arguments);

并在您的MainPage()MainPage.xaml.cs 中添加:

ShellViewFrame.Navigate(typeof(Shell));

或者,第二种方式,你可以参考GitHub中的这个Sample,这是一种更好的方式。

【讨论】:

  • 我检查了 GitHub 项目及其使用 AppShell 的方法,以及 @Ken Tucker 创建新 NavigationService 的解决方案为我解决了这个问题。谢谢大家!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多