title: SilverLight 4: Navigate to different frames in a SL control from Javascript
body: <p>I have a silverlight control that has a Frame on it. I want to change the URI of this frame from outside the SL control. (I have an HTML link that will use Javascript to ulimtately tell the SL control to change.) This is all working but I get random JavaScript errors.</p>
  
  <p>Master Page:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;a href="#" onclick="PdmcNav.NavigateTo('page1');"&gt;Page 1 Link&lt;/a&gt; 
      &lt;a href="#" onclick="PdmcNav.NavigateTo('page2');"&gt;Page 2 Link&lt;/a&gt; 
  
      &lt;div id="main" &gt;
          &lt;asp:ContentPlaceHolder ID="MainContent" runat="server"&gt;
          &lt;/asp:ContentPlaceHolder&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Included Javascript:</p>
  
  <pre><code>// 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);
      }
  }
  </code></pre>
  
  <p>Main Xaml page loaded in master page (MainNavigationView.xaml)</p>
  
  <pre><code>&lt;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"&gt;
      &lt;StackPanel&gt;
          &lt;!-- this is a test of navigation in the control.... works flawlessly --&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;HyperlinkButton Content="profile" Margin="4" TargetName="contentFrame" NavigateUri="/Views/SupplierProfile.xaml"/&gt;
              &lt;HyperlinkButton Content="scores" Margin="4" TargetName="contentFrame" NavigateUri="/Views/SupplierScores.xaml"/&gt;
          &lt;/StackPanel&gt;
          &lt;navigation:Frame x:Name="contentFrame"
                            Source="/Views/Profile.xaml"
                            VerticalAlignment="Stretch"
                            HorizontalAlignment="Stretch" /&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>MainNavigationView.xaml.cs</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Browser;
  using System;
  
  namespace PDMC.Views {
      public partial class MainNavigationView : UserControl {
          /// &lt;summary&gt;
          /// Initializes a new instance of the MainNavigationView class.
          /// &lt;/summary&gt;
          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);
              }
          }
      }
  }
  </code></pre>
  
  <p>I can click on the links in the master page a few times but after several clicks of going back and forth, i get the following error: (its random when i get this)</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClass3.&lt;InitializeNavigationState&gt;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)
  </code></pre>
  
  <p>Anybody see what Im doing wrong?</p>