UWP通过用户控件访问的页面导航框架的对象? [英] UWP Accessing Frame for page navigation through Usercontrol an Object?

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

问题描述

我目前正在开发一个应用程序UWP涉及内部地图的几个用户控件对象(的使用Windows.UI.Xaml.Navigation 的)。

I'm currently developing a UWP application that involves several Usercontrol objects inside of a Map (using Windows.UI.Xaml.Navigation).

与这些用户控件对象有时我会要求用户能够在物体按下一个按钮,被带到一个新的页面,唯一的问题是,我似乎无法访问该网页的框架中能够使用

with these Usercontrol objects I sometimes require the user to be able to press a button in the objects and be taken to a new page, the only issue is I can't seem to access the page's Frame to be able to use the

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



方法。我怎么会去这个和/或有什么办法?我一直停留在这大半天!

method. How would I go about this and/or are there any alternatives? I've been stuck on this most of the day!

预先感谢任何帮助你们可以提供!

Thanks in advance for any help you guys can 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. 在父页面,收听到用户控件的事件并调用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);
    }


}

导航通首页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通过用户控件访问的页面导航框架的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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