我正在开发一个 UWP 应用程序,涉及多个用户控制 https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.usercontrolMap 中的对象使用Windows.UI.Xaml.Navigation
.
有时,用户应该能够单击这些对象中的按钮来转到新页面。但是,我无法访问页面的框架,因此无法使用以下方法。
Frame.Navigate(typeof([page]));
我如何访问页面框架以使用该方法?
让我知道任何替代方案;我一整天都被困在这个问题上了!预先感谢你们提供的任何帮助!
我们可以让页面自行导航。只需在自定义用户控件中定义一个事件并在其父控件(页面)中监听该事件。
以下面为例:
- 创建一个自定义用户控件并在其上放置一个按钮以进行测试。
- 在测试按钮的单击事件中,引发该事件以导航父页面。
- 在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>
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);
}
}
导航主页到第二页:
public MainPage()
{
this.InitializeComponent();
myControl.OnNavigateParentReady += myControl_OnNavigateParentReady;
}
private void MyControl_OnNavigateParentReady(object source, EventArgs e)
{
Frame.Navigate(typeof(SecondPage));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)