I have two views (viewA and viewB), where I navigate between them in the mainwindow using navigationservice. I need to use the same instance of viewA after navigating multiple times.
how to use the same instance of the view in the navigationservice.navigate() in wpf
600 Views Asked by Galilo Galilo AtThere are 2 best solutions below
On
I would make this generic.
You don't say what navigationservce is or what the views are.
Either, maintain your list of views yourself.
Add a dictionary with Key of Type and value Page ( or object or whatever a view is in your app ).
When you navigate you can then navigate to a Type and check if there's already an entry in your dictionary using .ContainsKey(theType). If there's one there then navigate to that by passing it into your navigation process. If there's not one there then
Activator.CreateInstance(theType)
To create a page/view/whatever.
Add this to your dictionary and navigate to it.
Or
Use a dependency injection container such as unity to .Resolve a singleton for each view.
Either way, you may need some new method or changes to an existing one depending on what your navigation service does.
I am using an instance of
Pagefor navigating pages. EachPagecan have eachviewmodelto hold data. And, You just need to bind data if you want to update data fromviewmodelin real time.MainWindow xaml
MainWindow Behind Code
ViewA xaml
ViewA behind code
ViewB xaml
ViewB behind code