I will give you a workarount with a demo:
MainWindow.xaml
<Frame x:Name="MyFrame"></Frame>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="AboutButton_Click"/>
MainWindow.xaml.cs
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm currently working on a project that needs to navigate between a Window and a Page. I've managed to navigate between a Window to a Page using the following code:
public void AboutButton_Click(object sender, RoutedEventArgs e)
{
About about = new About();
this.Content = about.Content;
}
However, when I try to navigate back to my Window using this code:
public void HomeButton_Click(object sender, RoutedEventArgs e)
{
MainWindow home = new MainWindow();
this.Content = home;
}
It displays the error that is: System.InvalidOperationException: 'Window must be the root of the tree. Cannot add Window as a child of Visual.' I understand now that you cannot navigate back to a Window using a Page, however is there a workaround to this? Something I can implement without too much redesigning of my entire code? Please let me know if you need anymore information!
Reece
I will give you a workarount with a demo:
MainWindow.xaml
<Frame x:Name="MyFrame"></Frame>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="AboutButton_Click"/>
MainWindow.xaml.cs