This is probably one of those questions where the answer really should be too obvious for any to miss. Still, I can’t seem to figure out why my “play&learn” application behaves the way it does.
On my Mainpage.xaml I have a StackPanel containing several HyperlinkButtons which navigates to a set of NavigationPages. There is also a NavigationFrame with a UriMapper to hold the “pages”.
<StackPanel Background="Black" Orientation="Horizontal" Grid.Row="0">
<HyperlinkButton Name="Home"
TargetName="MainPageFrame" NavigateUri="/Home"
Foreground="White" FontWeight="Bold" Content="Home" />
<HyperlinkButton Name="Users"
TargetName="MainPageFrame" NavigateUri="/Users"
Foreground="White" FontWeight="Bold" Content="Users" />
<HyperlinkButton Name="Store" Foreground="White" FontWeight="Bold" Content="Store"
TargetName="MainPageFrame" NavigateUri="/Stores"/>
</StackPanel>
<navigation:Frame x:Name="MainPageFrame" Grid.Row="1" Source="/Home" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" JournalOwnership="Automatic">
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>
Here’s the problem. When I go back and forth between the pages (i.e: click on Stores, Users and back on Stores) then two Stores pages are created. Though not visible in the application at first glance the problem materialize itself when I a child window is opened from the Stores Page.
As I use MVVM light messaging to notify that the child window should open, … I get two child windows (or one for each time I have entered the stores navigation page from the hyperlinkbuttons).
I presumed that while clicking on the Hyperlink buttons, you would only have one NavigationPage ..or at least the current was destructed while leaving the navpage.
What clearly obvious thing am I missing?
The problem lies most likely in the registration of the message handler. There is a known problem with the MVVM Light Messenger, that results in the object handling a message not being released propperly.
The solution is quite simple – assuming that your view handles the message – your code behind should look something like this:
Now modify it so it looks similar to this:
The code in the unloaded event ensures that the message handler is properly unregistered. For messages in
ViewModelsensure that theCleanupmethod is called.