我正在开发一个涉及使用Windows.UI.Xaml.Navigation
的地图中包含多个UserControl对象的UWP应用程序。
有时,用户应该能够点击这些对象中的按钮以转到新页面。 但是,我无法访问页面的框架,因此无法使用以下方法。
Frame.Navigate(typeof([page]));
我该如何访问页面框架以使用这个方法?
如果有任何替代方案,请告诉我;我已经困扰了一整天!感谢您提供的任何帮助!
我正在开发一个涉及使用Windows.UI.Xaml.Navigation
的地图中包含多个UserControl对象的UWP应用程序。
有时,用户应该能够点击这些对象中的按钮以转到新页面。 但是,我无法访问页面的框架,因此无法使用以下方法。
Frame.Navigate(typeof([page]));
我该如何访问页面框架以使用这个方法?
如果有任何替代方案,请告诉我;我已经困扰了一整天!感谢您提供的任何帮助!
我们可以让页面自行导航。只需在您的自定义用户控件中定义一个事件,并在其父级(页面)中侦听该事件。
以以下内容为例:
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的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):
public MainPage()
{
this.InitializeComponent();
myControl.OnNavigateParentReady += myControl_OnNavigateParentReady;
}
private void MyControl_OnNavigateParentReady(object source, EventArgs e)
{
Frame.Navigate(typeof(SecondPage));
}
Frame navigationFrame = Window.Current.Content as Frame;
navigationFrame.Navigate(typeof([page]));
或者,使用 Cast=>
((Frame)Window.Current.Content).Navigate(typeof(Views.SecondPage));
public event
更改为public static event
,以便我可以在Page
中的Grid
中的GridView
中的GridView.ItemTemplate
中的DataTemplate
中的UserControl
中使用此解决方案... 感谢您提供的绝佳解决方案。 - dav1dsm1th