简体   繁体   English

使用Template10在页面内导航

[英]Navigation within Page using Template10

I have problem how to implement sub-page navigation in UWP. 我有问题如何在UWP中实现子页面导航。 The page is in RootFrame, which I can use on navigation. 该页面位于RootFrame中,我可以在导航中使用。 But i want to use something like this: 但我想使用这样的东西:

    <Page>
<Grid>
 <Frame x:Name="MyFrame"/>
</Grid>
</Page>

What I want is, use Navigate method of control MyFrame in ViewModel. 我想要的是,在ViewModel中使用控制MyFrame的Navigate方法。 I can call the method from code-behind, but I'm developing my app using MVVM. 我可以从代码隐藏调用该方法,但我正在使用MVVM开发我的应用程序。 I'm not sure, if Template10 can work with sub-frames. 我不确定,如果Template10可以使用子帧。

I appreciate any advice. 我很感激任何建议。

EDIT: More details: I have pivot control which is in page. 编辑:更多细节:我在页面中有枢轴控制。 the pivot has 2 tabs (pivotitems). 枢轴有2个选项卡(pivotitems)。 The content of the pivotitem must be navigable. pivotitem的内容必须是可导航的。 What I mean: I pivotitem 1, I need to have one Frame and use it for navigation in the pivotitem. 我的意思是:我的第一部分,我需要有一个框架并用它在枢轴实现中导航。 My problem is, how to use or how to call the frame in pivotitem from ViewModel, especially I need to call Navigate method. 我的问题是,如何使用或如何从ViewModel调用pivotitem中的帧,特别是我需要调用Navigate方法。 Now I'm using Template10's navigation service and it's working with rootframe. 现在我正在使用Template10的导航服务,它正在使用rootframe。 I don't know, how to use it for other let's say sub-frames. 我不知道,如何将其用于其他让我们说的子帧。

You can always do this. 你总是可以这样做。

var nav = Bootstrapper.NavigationServiceFactory(BackButton.Attach, ExistingContent.Exclude, this.Frame);

This will give you a navigation service for the frame in your page. 这将为您提供页面中框架的导航服务。 You can then use session state, if you like. 如果愿意,您可以使用会话状态。

Bootstapper.SessionState["MyNav"] = nav;

From here your view-model can access the service and navigate. 从这里,您的视图模型可以访问服务并导航。 You can repeat this for as many frames as you have. 您可以重复此操作以获得尽可能多的帧。 And you can then handle navigation in your view-model without consideration of "where" the frame is, just that your logic requires it to nav. 然后,您可以在视图模型中处理导航,而无需考虑框架的“位置”,只需要您的逻辑需要导航。

Does this make sense? 这有意义吗?

I don't know how you are going to trigger the navigation change so I'll assume it will start from a button click. 我不知道你将如何触发导航更改,所以我认为它将从按钮点击开始。 I am also assuming the button's Command property is already bound to an ICommand in the viewmodel (the same concepts can be applied to different kinds of views). 我还假设按钮的Command属性已经绑定到viewmodel中的ICommand (相同的概念可以应用于不同类型的视图)。

All we have to do now is to make the ICommand implementation call our custom NavigationService to perform the content switch. 我们现在要做的就是让ICommand实现调用我们的自定义NavigationService来执行内容切换。 This NavigationService class will be nothing but a simple proxy to the window global frame. 这个NavigationService类只不过是窗口全局框架的简单代理。 Its main navigation method can be as simples as: 它的主要导航方法可以简单如下:

    public void Switch()
    {
        var rootFrame = Window.Current.Content as Frame;

        if ((rootFrame.Content as ParentPage) != null)
        {
            rootFrame.Navigate(typeof(ChildPage));   
        }
    }

So you have tagged this with Template10 but it seems to be a more general question for UWP as a whole. 所以你用Template10标记了它,但它似乎是整个UWP的一个更普遍的问题。 I wonder if you have considered all of the inherent complexities with this approach - specifically related to suspension and resume. 我想知道你是否考虑过这种方法的所有固有的复杂性 - 特别是与暂停和恢复有关。 For each frame you have, you would need to save and restore navigation state, which isn't straight-forward when you have nested frames. 对于您拥有的每个帧,您需要保存和恢复导航状态,当您有嵌套帧时,这不是直接的。 Have you also considered how global navigation would work? 您是否也考虑过全球导航将如何运作?

Template 10 does support the concept of multiple NavigationServices and, therefore, multiple frames, but only from the perspective of you can create them. 模板10确实支持多个NavigationServices的概念,因此支持多个框架,但只有从您可以创建它们的角度来看。 Template10 does not inherently understand how such frames may be related to each other, so cannot perform automatic back propagation where you have something like: Template10本身并不了解这些帧如何相互关联,因此无法在以下情况下执行自动反向传播:

FrameA[Main->Page1->Page1:Pivot1.FrameB[View1->View2->View3]] FrameA [Main-> Page1->第1页:Pivot1.FrameB [View1-> View2-> VIEW3]]

Here we have two frames - FrameA and FrameB. 这里我们有两个框架 - FrameA和FrameB。 FrameA has navigated from Main to Page1. FrameA已从Main导航到Page1。 Page1 has a Pivot that hosts FrameB in PivotItem1 and FrameB has navigated from View1 to View 2 and from View2 to View 3. Page1有一个Pivot,在PivotItem1中托管FrameB,FrameB已经从View1导航到View 2,从View2导航到View 3。

Global navigation (ie the shell back, etc.) would be automatically wired to FrameA, so you would need to intercept that action, and then handle you own navigation activity for FrameB. 全局导航(即shell返回等)将自动连接到FrameA,因此您需要拦截该操作,然后处理您自己的FrameB导航活动。

Take a look at the BackButtonBehavior to see how it is possible to intercept the global back and then put in place your own action. 看一下BackButtonBehavior ,看看如何拦截全局回来然后放置你自己的动作。

我不知道你是否可以做那样的事情。一个可能的解决方法是使用一个Messenger从你的viewmodel发送消息到后面的视图代码..虽然我不是这个解决方案的粉丝,因为我在你必须使用页面代码之后说...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM