NavigationPage is incremented for each HyperlinkButton click (Silverlight)

情到浓时终转凉″ 提交于 2019-12-13 18:09:06

问题


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?


回答1:


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:

public StoreView() {
    Messenger.Default.Register<NotificationMessage>(this, (m) => {
        // some message handling
    });

    InitializeComponent();
}

Now modify it so it looks similar to this:

public StoreView() {
    Messenger.Default.Register<NotificationMessage>(this, (m) => {
        // some message handling
    });

    InitializeComponent();

    this.Unloaded += (sender, args) => {
        Messenger.Default.Unregister(this);
    };
}

The code in the unloaded event ensures that the message handler is properly unregistered. For messages in ViewModels ensure that the Cleanup method is called.



来源:https://stackoverflow.com/questions/6540139/navigationpage-is-incremented-for-each-hyperlinkbutton-click-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!