I am brand new to WPF (and .NET in general) and am writing code based on online samples and tutorials. Please bare with me if I sound ignorant of general knowledge since I only worked on native kernel code for the past 7 years.
Anyway, I am trying to write a desktop app that quires navigation and the best model I have so far is a Frame embedded in a NavigationWindow. There are times when only a portion of the GUI changes and other times when the whole thing changes. One of the state transitions is triggered by an event in a page hosted by the internal frame (like a button click) but I want to navigate the entire window to a different page. So far, I haven't found any examples doing this. I got it to work by pretty much locating the main window global variable. My code in the frame page is:
private void FramePageButton_Click(object sender, RoutedEventArgs e) { NavigationWindow mainWindow = (NavigationWindow)App.Current.MainWindow; mainWindow.Navigate(new MainPage2()); }
I am not a big fan of this since I think it breaks OO abstraction. The contents of the frame page should not be aware of the main window and how it transitions between states. A better approach is probably using a regular C# event/delegate but I am not sure how to hook it up nicely. The page in the frame gets loaded automatically when the app starts because it is declared in XAML. I guess one thing I can do is to instantiate that page manually in the host page's constructor and tell the frame to navigate to it. This way I can attach a callback to the page's delegate.
So, does anyone know what's the preferred approach? This feels to be a pretty standard pattern for navigation apps. Thank you very much.