2011-04-06 67 views
0

我有一個Silverlight控件,它有一個框架。我想從SL控件外部更改此幀的URI。 (我有一個HTML鏈接,它將使用Javascript來最終告訴SL控件改變。)這是所有的工作,但我得到隨機的JavaScript錯誤。SilverLight 4:從Javascript導航到SL控件中的不同框架

母版頁:

<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

看來我的問題的解決方案是改變我的方法。在我的研究中,我發現Main.xaml中的Frame元素,默認JournalOwnership設置爲Automatic。如果我將此設置爲OwnsJournal,問題就會消失。顯然,如果該框架正在使用瀏覽器日誌,如果您通過[ScriptableMethod]導航,則會發生奇怪的事情。

我的解決方案是改變我對待問題的方法....最終變得更加簡單和優雅。有一點需要注意的是,當日志由瀏覽器管理時(JournalOwnership=Automatic),只需使用URL即可導航至控件中的頁面。

下面是我最終的解決方案,它允許我在我的SL控件中導航到不同頁面的HTML導航(在我的silverlight控件之外)。

母版頁(純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控件的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> 

最後,爲了使外部鏈接簡單,漂亮,我添加了一個UriMapper到我的App.xaml:

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

這它..一個更簡單的解決方案..希望這可以幫助其他人在路上(是的,我是在這個發現時銀光新:))

+0

我也有類似的要求。我在我的Main.Xaml中也有一個框架(我將它命名爲「abc」) 這就是我創建silverlight對象的方式。 Silverlight.createObject( 「ClientBin/InSite.Shell。xap「,//源 document.getElementById('silverlightControlHost'),//父元素 」insiteSilverLight「,//生成的對象元素的ID爲 { 寬度:」100%「,高度:」100%「,背景: 「白」, 版本: 「4.0.60310.0」 },{ 的onLoad:onSLLoad} ); – lohiarahul 2016-07-04 13:41:33

+0

我上加載事件發件人 功能onSLLoad(發件人,EventArgs){ VAR rootFrame = sender.children [0]; } \t \t \t \t我希望在此框架上調用.NavigateTo('#my/path')。但是我不能。我如何得到孩子的類型。它只是作爲HtmlParamElement返回,我不能確定它是否是我要求的框架。 \t \t \t \t如何解析DOM並執行NavigateTo()方法? – lohiarahul 2016-07-04 13:41:43

相關問題