SilverLight 4:从Javascript导航到SL控件中的不同帧

时间:2011-04-06 13:53:57

标签: javascript silverlight-4.0 silverlight-toolkit

我有一个Silverlight控件,上面有一个Frame。我想从SL控件外部更改此框架的URI。 (我有一个HTML链接,它将使用Javascript来最终告诉SL控件改变。)这一切都正常,但我得到随机的JavaScript错误。

母版页:

<html>
<body>
    <a href="#" onclick="PdmcNav.NavigateTo('page1');">Page 1 Link</a> 
    <a href="#" onclick="PdmcNav.NavigateTo('page2');">Page 2 Link</a> 

    <div id="main" >
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
        </asp:ContentPlaceHolder>
    </div>
</body>
</html>

包含Javascript:

// Defining the namespace object for the Pdmc navigation
var PdmcNav = {};
PdmcNav.PdmcSLControl = null;

PdmcNav.NavigateTo = function (pagename) {

    // check to see if PDMC Silverlight control is on page. if not (is null), then need to load main PDMC page
    if (PdmcNav.PdmcSLControl == null) {
        // handle this later
    } else {
        // Pdmc SL control on page.. 
        // Talk to silverlight control and request it to navigate to pagename
        PdmcNav.PdmcSLControl.Content.PdmcSL.NavigateToPage(pagename);
    }
}

主页面中加载的主Xaml页面(MainNavigationView.xaml)

<UserControl x:Class="PDMC.Views.MainNavigationView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:toolkit="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
             xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
             xmlns:navigationCore="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
             mc:Ignorable="d">
    <StackPanel>
        <!-- this is a test of navigation in the control.... works flawlessly -->
        <StackPanel Orientation="Horizontal">
            <HyperlinkButton Content="profile" Margin="4" TargetName="contentFrame" NavigateUri="/Views/SupplierProfile.xaml"/>
            <HyperlinkButton Content="scores" Margin="4" TargetName="contentFrame" NavigateUri="/Views/SupplierScores.xaml"/>
        </StackPanel>
        <navigation:Frame x:Name="contentFrame"
                          Source="/Views/Profile.xaml"
                          VerticalAlignment="Stretch"
                          HorizontalAlignment="Stretch" />
    </StackPanel>
</UserControl>

MainNavigationView.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Browser;
using System;

namespace PDMC.Views {
    public partial class MainNavigationView : UserControl {
        /// <summary>
        /// Initializes a new instance of the MainNavigationView class.
        /// </summary>
        public MainNavigationView() {
            InitializeComponent();

            HtmlPage.RegisterScriptableObject("PdmcSL", this);
        }

        [ScriptableMember]
        public void NavigateToPage(string pageName) {

            if (pageName == "Profile") {
                Uri x = new Uri(@"/Views/Profile.xaml", System.UriKind.RelativeOrAbsolute);
                contentFrame.Source = x;//.Navigate(x);
            } else if (pageName == "Scores") {
                Uri x = new Uri(@"/Views/Scores.xaml", System.UriKind.RelativeOrAbsolute);
                contentFrame.Source=x;//.Navigate(x);
            }
        }
    }
}

我可以点击主页面中的链接几次,但经过几次来回点击后,我收到以下错误:(当我得到它时它是随机的)

Message: Unhandled Error in Silverlight Application Content for the URI cannot be loaded. The URI may be invalid.
Parameter name: uri   at System.Windows.Navigation.NavigationService.NavigateCore(Uri uri, NavigationMode mode, Boolean suppressJournalAdd, Boolean isRedirect)
   at System.Windows.Navigation.NavigationService.Journal_Navigated(Object sender, JournalEventArgs args)
   at System.Windows.Navigation.Journal.OnNavigated(String name, Uri uri, NavigationMode mode)
   at System.Windows.Navigation.Journal.UpdateObservables(JournalEntry currentEntry, NavigationMode mode)
   at System.Windows.Navigation.Journal.AddHistoryPoint(JournalEntry journalEntry)
   at System.Windows.Navigation.Journal.AddHistoryPointIfDifferent(String newState)
   at System.Windows.Navigation.Journal.Browser_Navigated(Object sender, EventArgs eventArgs)
   at System.Windows.Navigation.Journal.<>c__DisplayClass3.<InitializeNavigationState>b__2(Object sender, NavigationStateChangedEventArgs args)
   at System.Windows.Interop.SilverlightHost.RaiseNavigationStateChanged(String oldState, String newState)
   at System.Windows.Interop.SilverlightHost.OnNavigationStatePollingTick(Object sender, EventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

有人看到我做错了吗?

1 个答案:

答案 0 :(得分:1)

我的问题的解决方案似乎是改变我的方法。在我的研究过程中,我发现对于Main.xaml中的Frame元素,默认JournalOwnership设置为Automatic。如果我将其设置为OwnsJournal,则问题就会消失。显然,如果框架正在使用浏览器的日志,如果您通过[ScriptableMethod]导航,则会发生奇怪的事情。

我的解决方案是改变我对问题的态度......最终变得更加简单和优雅。需要注意的一点是,当日记由浏览器管理时(JournalOwnership=Automatic),您只需使用URL即可导航到控件中的页面。

这是我最终得到的解决方案,它允许我使用HTML导航(在我的silverlight控件之外),它将导航到SL控件中的不同页面。

母版页(带有导航链接的普通Html)

<html>
<body>
    <asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/PDMC.aspx#Profiles">Profiles</asp:HyperLink>
    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/PDMC.aspx#Scores">Scores</asp:HyperLink>
</body>
</html>

注意,PDMC.aspx是一个包含我的silverlight控件对象的简单页面。

Main.xaml是我的silverlight Control的RootVisual。它只包含一个框架,我们将用它来交换视图:

<navigation:Page x:Class="PDMC.Views.Main" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    d:DesignWidth="640" d:DesignHeight="480"
    Title="Main Page">    
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <StackPanel x:Name="LayoutRoot">
                <navigation:Frame x:Name="MainFrame" 
                                  Source="/Views/Profile.xaml"
                                  JournalOwnership="Automatic"
                                  UriMapper="{StaticResource PDMC_UriMapper}" />
            </StackPanel>
        </ScrollViewer>
</navigation:Page>

最后,为了使外部链接更简单,更漂亮,我在我的App.xaml中添加了一个UriMapper:

<Application.Resources>
    <navigationCore:UriMapper x:Key="PDMC_UriMapper">
        <navigationCore:UriMapping Uri="Profiles" MappedUri="/Views/Profile.xaml" />
        <navigationCore:UriMapping Uri="Scores" MappedUri="/Views/Scores.xaml" />            
    </navigationCore:UriMapper>
</Application.Resources>

这就是..一个更简单的解决方案..希望这有助于其他人在路上(并且在发现这个时我是银光的新手:))