如何访问页面框架以通过 UWP 中的 UserControl 对象导航页面? [英] How do I access a page frame to navigate a page through a UserControl object in a UWP?

查看:25
本文介绍了如何访问页面框架以通过 UWP 中的 UserControl 对象导航页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 UWP 应用程序,它涉及多个 UserControl 使用 Windows.UI.Xaml.Navigation 的 Map 对象.

I'm developing a UWP application that involves several UserControl objects inside a Map using Windows.UI.Xaml.Navigation.

有时,用户应该能够单击这些对象中的按钮以转到新页面.但是,我无法访问页面的框架,因此无法使用以下方法.

Sometimes, the user should be able to click a button in these objects to go to a new page. However, I can't access the page's frame, so I can't use the following method.

Frame.Navigate(typeof([page])); 

如何访问页面框架以使用该方法?

How could I access the page frame to use the method?

让我知道任何替代方案;我一天中的大部分时间都被困在这个问题上!提前感谢你们提供的任何帮助!

Let me know of any alternatives; I've been stuck on this for most of the day! Thanks in advance for any help you guys offer!

推荐答案

我们可以让页面自行导航.只需在您的自定义用户控件中定义一个事件并在其父级(页面)中监听该事件.

We can let the page to navigate itself. Just define an event in your custom usercontrol and listen to the event in its parent(the page).

以下面的例子为例:

  1. 创建一个自定义用户控件并在其上放置一个按钮以进行测试.
  2. 在测试按钮的点击事件中,引发事件以导航父页面.
  3. 在父页面中,监听 UserControl 的事件并调用 Frame.Navigate.

MyControl 的 Xaml:

MyControl's Xaml:

<UserControl
x:Class="App6.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App6"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
    <Button x:Name="testbtn" Margin="168,134,0,134" Click="testbtn_Click">test</Button>
</Grid>
</UserControl>

MyControl 的代码隐藏:

MyControl's CodeBehind:

public sealed partial class MyControl : UserControl
{

    public delegate void MyEventHandler(object source, EventArgs e);

    public event MyEventHandler OnNavigateParentReady;

    public MyControl()
    {
        this.InitializeComponent();
    }

    private void testbtn_Click(object sender, RoutedEventArgs e)
    {
        OnNavigateParentReady(this, null);
    }


}

将 MainPage 导航到 SecondPage:

Navigate MainPage to SecondPage:

    public MainPage()
    {
        this.InitializeComponent();

        myControl.OnNavigateParentReady += myControl_OnNavigateParentReady;
    }

    private void MyControl_OnNavigateParentReady(object source, EventArgs e)
    {
        Frame.Navigate(typeof(SecondPage));
    }

这篇关于如何访问页面框架以通过 UWP 中的 UserControl 对象导航页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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