从父页面启动导航时,如何在 UWP 页面之间传递参数? [英] How to pass a parameter between UWP pages when navigation is initiated from a parent page?

查看:71
本文介绍了从父页面启动导航时,如何在 UWP 页面之间传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的导航模型有一个 MainPage,它在 splitview 中包含一个汉堡包菜单和一个 MyFrame.我有两个页面,WorkingPage 和 SavePage,它们显示在 MyFrame 中.所以 MainPage 包括这个:

I am using a navigation model that has a MainPage, which contains a hamburger menu and a MyFrame in a splitview. I have two pages, WorkingPage and SavePage, which are displayed in the MyFrame. So MainPage includes this:

<Page>
    <!-- Other stuff -->

    <SplitView>
        <SplitView.Pane>
            <!-- Other stuff -->

            <ListBox Name="HamburgerMenuMenuItems"
                     SelectionChanged="HamburgerMenuMenuItems_SelectionChanged">

                    <ListBoxItem Name="HamburgerMenuItemSave">
                        <!-- Content -- >
                    </ListBoxItem>
            </ListBox>

            <!-- Other stuff -->
        </SplitView.Pane>

        <SplitView.Content>
            <Frame Name="MyFrame"></Frame>
        </SplitView.Content>
    </SplitView>

    <!-- Other stuff -->
</Page>

用户在 MainPage 上单击 Save,它是 Hamburger 菜单中的一项(设置为列表框),并引发选择更改事件,这导致 MainPage 启动从 WorkingPage 到 SavePage 的导航我的框架.

The user clicks Save, which is one of the items in the Hamburger menu (set up as a listbox), and which raises the selection changed event, on the MainPage, which results in MainPage initiating a navigation from WorkingPage to SavePage in MyFrame.

public sealed partial class MainPage : Page
{
    private void HamburgerMenuMenuItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Other options similar ...
        else if (HamburgerMenuItemSave.IsSelected)
        {
            MyFrame.Navigate(typeof(Pages.File.SavePage));
        }
        // Other options similar ...
    }
}

WorkingPage 包含我希望 SavePage 有权访问的数据成员.

WorkingPage contains a data member that I want SavePage to have access to.

这就是我所拥有的:

public sealed partial class WorkingPage : Page
{
    public MyClass myClass;
    // Other stuff ...
}

我希望将myClass"的值传递给 SavePage,因此最终结果为:

I want the value of "myClass" to be passed to SavePage, so it ultimately ends up as:

public sealed partial class SavePage : Page
{
    public MyClass myClass;
    // Other stuff ...
}

我从研究中知道(并且自己广泛使用它),在两个页面之间传递参数的正确方法如下:

I know from research (and extensively using it myself) that the proper way to pass parameters between two pages is as follows:

Frame.Navigate(typeof(PageClass), someParameter);

但是问题是MainPage是导航开始的地方,但是需要传递的参数值(myClass)只存在于WorkingPage的范围内.因此,这需要:

But the problem is MainPage is where the navigation is initiated, but the needed parameter value to pass (myClass) only exists in the scope of WorkingPage. This thus requires that either:

  • A) 导航由 WorkingPage 启动,以便我可以使用上面的代码行,只需将SavePage"作为 PageClass 和myClass"作为参数,或
  • B) MainPage 以某种方式需要获取myClass"值的知识,以便我可以使用与 (A) 相同的代码行,但使用this.MyFrame"而不是Frame"

如何使用从 MainPage 启动的导航事件从 WorkingPage 到 SavePage 获取myClass"的值?这似乎是一个普遍的需求,但我发现的所有内容都只讨论了一个页面启动导航到另一个页面的简单情况,此时参数必须从启动页面传递到另一个页面.

How can getting the value of "myClass" from WorkingPage to SavePage be accomplished, with a navigate event initiated from MainPage? This seems like a common need, but everything I have found only talks about the simple case of one page initiating navigation to another, when a parameter must be passed from the initiating page to the other.

推荐答案

我很确定这可以通过不同的方式完成..但我个人喜欢使用以下模式来实现这一点:

I am pretty sure this can be done in different ways.. but personally I like to use following pattern to achieve this :

首先,在您的 MainPage 中,您必须为导航创建一种 utility 方法:

Firstly, in your MainPage you have to create sort of an utility method for the navigation :

public void navigateWithParameter(Page yourPage,String yourParameter){
 MyFrame.Navigate(typeof(yourPage), yourParameter);
}

接下来,您可以通过获取当前实例任何页面(在您的情况下是WorkingPage)调用此方法> MainPage 并使用适当的参数调用 navigateWithParameter 函数:

Next you can call this method from any page you want to (which in your case is the WorkingPage), by getting the current instance of the MainPage and calling the navigateWithParameter function with the appropriate parameters :

var frame = (Frame)Window.Current.Content;
var mainpage = (MainPage)frame.Content;
mainpage.navigateWithParameter(yourPage,"It works!");

希望这会有所帮助!

这篇关于从父页面启动导航时,如何在 UWP 页面之间传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆