我目前正在开发一个UWP应用程序,它涉及Map中的几个Usercontrol对象(使用Windows.UI.Xaml.Navigation).
使用这些Usercontrol对象我有时需要用户能够按下对象中的按钮并被带到新页面,唯一的问题是我似乎无法访问页面的框架以便能够使用
Frame.Navigate(typeof([page]));
Run Code Online (Sandbox Code Playgroud)
方法.我怎么会这样做和/或有其他选择吗?我大部分时间都被困在这一天!
在此先感谢您提供的任何帮助!
Ala*_*SFT 12
我们可以让页面自行导航.只需在自定义用户控件中定义一个事件,然后在其父级(页面)中侦听该事件.
以下面的例子为例:
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)
小智 6
您可以从当前窗口的内容中获取对框架的引用。在您的用户控件的代码后面尝试:
Frame navigationFrame = Window.Current.Content as Frame;
navigationFrame.Navigate(typeof([page]));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13536 次 |
最近记录: |