In a project there are lots of Page
derived classes and
a MainWindow
that is a NavigationWindow
.
Should there really be Navigate(new PageDerivedClass())
in the code or should the hierarchical Page
tree be built
before?
If I go back and do the same thing again to create a new
PageDerivedClass
, shouldn't the application somehow
handle that case and use the already created page instead
of building a new one?
As far as I can see, the newly created page isn't GarbageCollected
,
since you still can Navigate through the arrows of the NavigationWindow
.
Is it leaking somehow?
How to use it the correct way? The MSDN is not very helpful here.
NavigationWindow does not store an instance of a content object in navigation history. Instead, NavigationWindow creates a new instance of the content object each time it is navigated to by using navigation history. This behavior is designed to avoid excessive memory consumption when large numbers and large pieces of content are being navigated to. Consequently, the state of the content is not remembered from one navigation to the next. However, WPF provides several techniques by which you can store a piece of state for a piece of content in navigation history. (Source MSDN)
If you really want to avoid creating new everytime, You can also create instances of various pages (Page1, Page2, etc) and store them in your Application object, then switch to them like this:
NavigationSerivce.Navigate(App.Page2);
Soruce: Stackoverflow answer