通过Usercontrol对象进行页面导航的UWP访问框架?

kgy*_*yts 12 c# wpf xaml uwp

我目前正在开发一个UWP应用程序,它涉及Map中的几个Usercontrol对象(使用Windows.UI.Xaml.Navigation).

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

Frame.Navigate(typeof([page])); 
Run Code Online (Sandbox Code Playgroud)

方法.我怎么会这样做和/或有其他选择吗?我大部分时间都被困在这一天!

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

Ala*_*SFT 12

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

以下面的例子为例:

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

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>
Run Code Online (Sandbox Code Playgroud)

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


}
Run Code Online (Sandbox Code Playgroud)

将MainPage导航到SecondPage:

    public MainPage()
    {
        this.InitializeComponent();

        myControl.OnNavigateParentReady += myControl_OnNavigateParentReady;
    }

    private void MyControl_OnNavigateParentReady(object source, EventArgs e)
    {
        Frame.Navigate(typeof(SecondPage));
    }
Run Code Online (Sandbox Code Playgroud)

  • 我需要将`public event`更改为`public static event`,这样我就可以在`GridConmplate`中的`UserControl`中使用`GridConmp``在`GridView`中的`GridView`中``GridCon``一个`Page` ...感谢一个很好的解决方案. (2认同)

小智 6

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

Frame navigationFrame = Window.Current.Content as Frame;
navigationFrame.Navigate(typeof([page]));
Run Code Online (Sandbox Code Playgroud)