首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >银光NavigationService总是空的

银光NavigationService总是空的
EN

Stack Overflow用户
提问于 2010-04-26 09:07:48
回答 2查看 2.9K关注 0票数 3

我读到有几个人在这个问题上有问题,所以我想发布一个(有点)优雅的解决方案,我是在尝试处理这个问题时想出的。问题是当您在Silverlight中创建模板化页面时,而ContentControls没有父框架的NavigationService (当您尝试使用它时,它总是为空)。在类似的场景中,NavigationService在智能中存在,但始终为null。若要启用站点范围内的导航:

'RootFrame').

  • Inside

  • 创建了一个新的UserControl (我称之为“NavFrame”),其中包含一个导航框架(我将其命名为
  1. --此框架可以设置任何您喜欢的内容。
  2. 将此UserControl设置为App.xaml.cs中的RootVisual (即,在您可以键入的任何页面中使用NavigationService ):

((NavFrame)App.Current.RootVisual).RootFrame.NavigationService .Navigate(新Uri(“您的Uri",UriKind.RelativeOrAbsolute)); )

EN

回答 2

Stack Overflow用户

发布于 2012-01-28 19:30:33

您可以创建一个Action,并将其拖到要使导航发生的控件之上,就像下面这样:

代码语言:javascript
复制
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。

票数 1
EN

Stack Overflow用户

发布于 2011-12-10 13:42:31

代码语言:javascript
复制
((Frame)(Application.Current.RootVisual as MainPage).FindName("ContentFrame"))
    .Navigate(new Uri("Page Name", UriKind.Relative));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2712218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档