Reputation: 1051
I have a frame in MainWindow
. I have a page called MainPage
from which I am trying to navigate away to another page
.
Here is my Xaml:
<Window x:Class="Lab_Lite.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
<DockPanel LastChildFill="True">
<Frame x:Name="Navigator" Source="/Lab_Lite;component/Pages/MainPage.xaml" />
</DockPanel>
</Window>
Here is my MainPage.cs file:
public partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
}
private void Tile_Click(object sender, RoutedEventArgs e)
{
using (Lab_Lite_Entities db = new Lab_Lite_Entities())
{
MainWindow test = new MainWindow();
string pageName = (from t in db.TypePages
select t)
.Where(t => t.Value.Replace("***", Environment.NewLine) == ((Button)sender).Content)
.Select(t => t.AssociatedPage)
.FirstOrDefault();
test.Navigator.Navigate(new Uri("Pages/" + pageName + ".xaml", UriKind.RelativeOrAbsolute));
}
}
}
When I debug my code I found that pagename
has perfect value that I am looking for. But I cannot navigate. I am not getting any errors.
Upvotes: 0
Views: 427
Reputation: 81313
You are creating all together new instane of MainWindow
which is not in View.
Use existing instance of MainWindow
which you can get via two approaches -
If MainWindow is startup window
:
MainWindow test = (MainWindow)App.Current.MainWindow;
Also, you can use this generic way too -
MainWindow test = (MainWindow)Window.GetWindow(this);
Upvotes: 1
Reputation: 8706
I haven't used frame navigation, but it seems that creating a new MainWindow and calling Navigate off it is the wrong thing to do. You should be calling Navigate off the actual existing MainWindow.
Upvotes: 1