我看到一些人在使用Silverlight创建模板页面时遇到了问题,ContentControls没有父级Frame的NavigationService(当您尝试使用它时,它总是为null)。还有类似情况,Intellisence中存在NavigationService但始终为null。以下是我解决此问题时想出的一个比较优雅的解决方案。要实现整个网站的导航:
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));