There are a some issues with the approaches you are using to switch between views.
In the first approach, you are trying to access the views in the region using their positions in the collection.
var region = regionManager.Regions["mainContentRegion"];
When the first view is deactivated it is being removed from the region automatically (because you are returning false in the getter of the
property) and therefore, the collection will no longer have two views. This means that accessing the element at index 1 in the next line will throw an exception, return a different view (if there were other views in the region besides
the two you mention above) or have other undefined behavior.
However, even if the view were not removed from the collection, it is not a recommended practice to access the views through their position in the collection.
In the second approach you are trying to use the RequestNavigate
However, take into account that the string you obtain from typeof(MusicGeneration).FullName
might be different from the one obtained from
(supposing that view is an instance of a
view.) To perform a navigation request the string passed as the view name should be the same as the one used when the view was registered with the
Also, if the view is already in the region and its IsNavigationTarget
method is returning false then the navigation request will disregard the existing view and create a new instance of it.
Based on the code you provided I think that the easier way to switch from the
to the MusicGeneration
view is invoking the
view like this:
With this, any other view in the region should be deactivated (as the region is used in a
) and removed (if their KeepAlive
property returns false) and the second view should be shown in its place (the existing view (if any) if its
returns true or a new instance otherwise.)