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

编程入门 行业动态 更新时间:2024-10-25 06:30:28
本文介绍了每次单击HyperlinkBut​​ton时,NavigationPage都会增加(Silverlight)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能是答案中的一个问题,对于任何人来说都不容错过.仍然,我似乎无法弄清楚为什么我的播放和学习"应用程序表现出它的行为方式.

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.

在我的Mainpage.xaml上,我有一个StackPanel,其中包含多个HyperlinkButtons,这些导航到一组NavigationPages.还有一个NavigationFrame和一个UriMapper来保存页面".

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.

当我使用MVVM轻信息通知要打开子窗口时,...我得到了两个子窗口(或每次从超链接按钮进入商店导航页面时都得到一个).

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

我认为,在单击超链接"按钮时,您将只有一个NavigationPage ..或者至少在离开导航页面时当前页面被破坏了.

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?

推荐答案

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

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); }; }

unloaded事件中的代码可确保正确注销消息处理程序.对于ViewModels中的消息,请确保调用Cleanup方法.

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.

更多推荐

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

本文发布于:2023-10-22 22:28:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   单击   HyperlinkBut   ton   NavigationPage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!