每次单击 HyperlinkBut​​ton 时,NavigationPage 都会递增 (Silverlight)

发布于 2024-11-18 08:10:45 字数 1659 浏览 3 评论 0原文

这可能是答案显而易见的问题之一,任何人都不会错过。尽管如此,我似乎还是无法弄清楚为什么我的“play&learn”应用程序的行为如此。

在我的 Mainpage.xaml 上,我有一个 StackPanel,其中包含多个 HyperlinkBut​​ton,可导航到一组 NavigationPages。还有一个带有 UriMapperNavigationFrame 来保存“页面”。

<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>

问题就在这里。当我在页面之间来回切换时(即:单击“商店”、“用户”,然后返回“商店”),则会创建两个“商店”页面。虽然乍一看在应用程序中不可见,但当我从商店页面打开子窗口时,问题就会出现。

当我使用 MVVM 轻消息传递来通知子窗口应该打开时,...我得到两个子窗口(或者每次我从超链接按钮进入商店导航页面时都会得到一个子窗口)。

我猜想,当单击“超链接”按钮时,您只会有一个导航页面..或者至少当前的导航页面在离开导航页面时被破坏。

我错过了什么明显的事情?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-11-25 08:10:45

问题很可能出在消息处理程序的注册上。 MVVM Light Messenger 存在一个已知问题,导致处理消息的对象无法正确发布。

解决方案非常简单 - 假设您的视图处理消息 - 您的后面的代码应该如下所示:

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

    InitializeComponent();
}

现在修改它,使其看起来与此类似:

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

    InitializeComponent();

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

unloaded 事件中的代码确保消息处理程序已正确取消注册。对于 ViewModels 中的消息,请确保调用 Cleanup 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文