Silverlight NavigationService 始终为空

发布于 2024-08-30 02:57:22 字数 681 浏览 5 评论 0原文

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

  1. 创建一个新的UserControl(我将其称为“NavFrame”),其中包含一个导航框架(我将其称为“RootFrame”)。

  2. 在这个框架内,您可以设置任何您喜欢的内容。

  3. 将此 UserControl 设置为 App.xaml.cs 中的 RootVisual(即 this.RootVisual = new NavFrame();)。

  4. 要在任何页面中使用 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:

  1. Create a new UserControl (I called mine 'NavFrame') that has a Navigation Frame in it (I called mine 'RootFrame').

  2. Inside this Frame you can set whatever content you like.

  3. Set this UserControl as your RootVisual in App.xaml.cs (i.e. this.RootVisual = new NavFrame();).

  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-09-06 02:57:22

您可以创建一个操作并将其拖动到要进行导航的控件顶部,就像这样:

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。

You can create an Action and drag it on top of the control you want to make navigation happen, just like this one:

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;
    }
}

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.

凹づ凸ル 2024-09-06 02:57:22
((Frame)(Application.Current.RootVisual as MainPage).FindName("ContentFrame"))
    .Navigate(new Uri("Page Name", UriKind.Relative));
((Frame)(Application.Current.RootVisual as MainPage).FindName("ContentFrame"))
    .Navigate(new Uri("Page Name", UriKind.Relative));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文