我读到有几个人在这个问题上有问题,所以我想发布一个(有点)优雅的解决方案,我是在尝试处理这个问题时想出的。问题是当您在Silverlight中创建模板化页面时,而ContentControls没有父框架的NavigationService (当您尝试使用它时,它总是为空)。在类似的场景中,NavigationService在智能中存在,但始终为null。若要启用站点范围内的导航:
'RootFrame').
UserControl (我称之为“NavFrame”),其中包含一个导航框架(我将其命名为RootVisual (即,在您可以键入的任何页面中使用NavigationService ):((NavFrame)App.Current.RootVisual).RootFrame.NavigationService .Navigate(新Uri(“您的Uri",UriKind.RelativeOrAbsolute)); )
发布于 2012-01-28 19:30:33
您可以创建一个Action,并将其拖到要使导航发生的控件之上,就像下面这样:
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;component/Page1.xaml“或框架上的UriMapping。
发布于 2011-12-10 13:42:31
((Frame)(Application.Current.RootVisual as MainPage).FindName("ContentFrame"))
.Navigate(new Uri("Page Name", UriKind.Relative));https://stackoverflow.com/questions/2712218
复制相似问题