Silverlight NavigationService 始终为空
我读到有些人遇到了这个问题,所以我想发布一个我在尝试处理这个问题时想到的(有点)优雅的解决方案。问题是当您在 Silverlight 中创建模板化页面时,ContentControls 没有父 Frame 的 NavigationService(当您尝试使用它时它始终为 null)。也有类似的场景,其中 NavigationService 存在于智能中,但始终为空。要启用站点范围的导航:
创建一个新的
UserControl
(我将其称为“NavFrame”),其中包含一个导航框架(我将其称为“RootFrame”)。在这个框架内,您可以设置任何您喜欢的内容。
将此 UserControl 设置为 App.xaml.cs 中的
RootVisual
(即this.RootVisual = new NavFrame();
)。要在任何页面中使用 NavigationService,您可以输入如下内容:
((NavFrame)App.Current.RootVisual).RootFrame.NavigationService .Navigate(new Uri("你的 Uri", UriKind.RelativeOrAbsolute));
I read that a few people were having a problem with this so I wanted to post a (somewhat) elegant solution I came up with while trying to deal with this. The problem is when you create templated pages in Silverlight and the ContentControls do not have the parent Frame's NavigationService (it's always null when you try and use it). There are similar scenarios where a NavigationService is present in intellisence but is always null. To enable site-wide Navigation:
Create a new
UserControl
(I called mine 'NavFrame') that has a Navigation Frame in it (I called mine 'RootFrame').Inside this Frame you can set whatever content you like.
Set this UserControl as your
RootVisual
in App.xaml.cs (i.e.this.RootVisual = new NavFrame();
).To use the NavigationService in any of your pages you can type something like:
((NavFrame)App.Current.RootVisual).RootFrame.NavigationService .Navigate(new Uri("Your Uri", UriKind.RelativeOrAbsolute));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个操作并将其拖动到要进行导航的控件顶部,就像这样:
现在您只需拖动它并将其连接到目标页面即可。顺便说一句,SL4 是这样,从未在 SL3 上尝试过。并且 URI 确实以以下形式工作:“/SilverlightApplication1;component/Page1.xaml”或使用框架上的 UriMapping。
You can create an Action and drag it on top of the control you want to make navigation happen, just like this one:
Now you just have to drag it and wire it to your target page. BTW this is true for SL4, never tried it on SL3. and the URI does work in the form: "/SilverlightApplication1;component/Page1.xaml" or with UriMapping on the Frame.