wpf UWP 访问框架以通过用户控制对象进行页面导航?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32254859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:22:17  来源:igfitidea点击:

UWP Accessing Frame for page navigation through Usercontrol an Object?

c#wpfxamluwp

提问by kgyts

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

我目前正在开发一个 UWP 应用程序,它涉及地图内的几个 Usercontrol 对象(使用 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

使用这些 Usercontrol 对象,我有时需要用户能够按下对象中的按钮并被带到一个新页面,唯一的问题是我似乎无法访问页面的 Frame 以能够使用

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!

在此先感谢你们提供的任何帮助!

采纳答案by Alan Yao - MSFT

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).

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

Take the following as an example:

以以下为例:

  1. Create a custom user control and put a button on it for test purpose.
  2. In test button's click event, raise the event to navigate parent page.
  3. In Parent page, listen to the UserControl's event and call Frame.Navigate.
  1. 创建一个自定义用户控件并在其上放置一个按钮以进行测试。
  2. 在测试按钮的单击事件中,引发事件以导航父页面。
  3. 在 Parent 页面中,监听 UserControl 的事件并调用 Frame.Navigate。

MyControl's Xaml:

MyControl 的 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's CodeBehind:

MyControl 的代码隐藏:

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);
    }


}

Navigate MainPage to SecondPage:

将 MainPage 导航到 SecondPage:

    public MainPage()
    {
        this.InitializeComponent();

        myControl.OnNavigateParentReady += myControl_OnNavigateParentReady;
    }

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

回答by meanme

You could get a reference to the Frame from the Current Window's Content. In your user control's code behind try:

您可以从当前窗口的内容中获取对框架的引用。在您的用户控件后面的代码中尝试:

Frame navigationFrame = Window.Current.Content as Frame;
navigationFrame.Navigate(typeof([page]));

回答by tvlada73

Or, with Cast=>

或者,使用 Cast=>

((Frame)Window.Current.Content).Navigate(typeof(Views.SecondPage));

((Frame)Window.Current.Content).Navigate(typeof(Views.SecondPage));