我读到有些人遇到了这个问题,所以我想发布一个我在尝试处理这个问题时想到的(有点)优雅的解决方案。问题是当您在 Silverlight 中创建模板化页面时,ContentControls 没有父 Frame 的 NavigationService(当您尝试使用它时它始终为 null)。也有类似的场景,其中 NavigationService 存在于智能中,但始终为空。要启用站点范围的导航:
创建一个新的UserControl
(我将我的称为“NavFrame”),其中有一个导航框架(我将我的称为“RootFrame”)。
在此框架内您可以设置任何您喜欢的内容。
将此 UserControl 设置为您的RootVisual
在 App.xaml.cs 中(即this.RootVisual = new NavFrame();
).
-
要在任何页面中使用 NavigationService,您可以键入以下内容:
((NavFrame)App.Current.RootVisual).RootFrame.NavigationService
.Navigate(new Uri("Your Uri", UriKind.RelativeOrAbsolute));
您可以创建一个操作并将其拖动到要进行导航的控件顶部,就像这样:
public class NavigateAction : TriggerAction<DependencyObject>
{
public Uri Uri
{
get;
set;
}
protected override void Invoke(object parameter)
{
var frame = FindContainingFrame(AssociatedObject);
if(frame == null)
throw new InvalidOperationException("Could not find the containing Frame in the visual tree.");
frame.Navigate(Uri);
}
protected static Frame FindContainingFrame(DependencyObject associatedObject)
{
var current = associatedObject;
while(!(current is Frame))
{
current = VisualTreeHelper.GetParent(current);
if(current == null)
return null;
}
return (Frame)current;
}
}
现在您只需将其拖动并将其连接到目标页面即可。顺便说一句,SL4 是这样,从未在 SL3 上尝试过。并且 URI 确实以以下形式工作:“/SilverlightApplication1;组件/Page1.xaml" 或使用框架上的 UriMapping。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)