UWP Frame access for page navigation through Usercontrol Object?

I am currently developing a UWP application that includes several Usercontrol objects inside a Map (using Windows.UI.Xaml.Navigation).

with these Usercontrol objects, I sometimes require that the user can click the button in the objects and go to a new page, the only problem is that I can’t access the page frame in order to be able to use

Frame.Navigate(typeof([page])); 

method. How can I do this and / or are there alternatives? I am stuck with this for most of the day!

Thanks in advance for any help you can offer the guys!

+6
source share
3 answers

We can let the page navigate. Just define the event in your user control and listen to the event in its parent (page).

As an example, take the following:

  • Create a custom control and place a button on it for testing.
  • In the test window, click an event, raise an event to go to the parent page.
  • On the parent page, listen for the UserControl event and call 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 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); } } 

Navigate MainPage to SecondPage:

  public MainPage() { this.InitializeComponent(); myControl.OnNavigateParentReady += myControl_OnNavigateParentReady; } private void MyControl_OnNavigateParentReady(object source, EventArgs e) { Frame.Navigate(typeof(SecondPage)); } 
+7
source

You can get a link to the frame from the contents of the current window. In your user control code for trying:

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

Or using Cast =>

((frame) Window.Current.Content) .Navigate (TypeOf (Views.SecondPage));

0
source

All Articles