每次单击 HyperlinkButton 时,NavigationPage 都会递增 (Silverlight)
这可能是答案显而易见的问题之一,任何人都不会错过。尽管如此,我似乎还是无法弄清楚为什么我的“play&learn”应用程序的行为如此。
在我的 Mainpage.xaml
上,我有一个 StackPanel
,其中包含多个 HyperlinkButton
,可导航到一组 NavigationPages
。还有一个带有 UriMapper
的 NavigationFrame
来保存“页面”。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题很可能出在消息处理程序的注册上。 MVVM Light Messenger 存在一个已知问题,导致处理消息的对象无法正确发布。
解决方案非常简单 - 假设您的视图处理消息 - 您的后面的代码应该如下所示:
现在修改它,使其看起来与此类似:
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:
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
ViewModels
ensure that theCleanup
method is called.