using System; using System.Windows.Threading; using System.Windows.Browser; using System.Diagnostics; namespace NerdPlusArt { public static class jQueryHistoryHelper { private static DispatcherTimer timer; private static string _prevBookmark; private static string _debugMessage = "Unable to execute the needed jQuery.History function. jQuery and the jQuery.history plugin both need to be accessible from the html page that hosts your Silverlight object in order for this to rock and roll. Details are available here: http://www.mikage.to/jquery/jquery_history.html"; static jQueryHistoryHelper() { // This is where we initialize the jQuery.history plugin. We're initializing it // with a blank callback function because we will use a timer to get updates to // the current bookmark rather than relying on the javascript callbacks. Registering // for the callback may be a better approach but I took this approach to avoid needing // to insert javascript into the DOM and also because it's 1:27 AM. try { HtmlPage.Window.Eval("$(document).ready(function() { $.historyInit(function pageload(hash) { }); });"); } catch (System.ArgumentException e) { Debug.WriteLine(_debugMessage); } // Initialize the timer that will watch for for changes to the current bookmark. timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(200); timer.Tick += new EventHandler(timer_Tick); timer.Start(); } static void timer_Tick(object sender, EventArgs e) { string _currentBookmark = HtmlPage.Window.CurrentBookmark; if (!_currentBookmark.Equals(_prevBookmark)) { RaiseNavigationRequested(_prevBookmark, _currentBookmark); _prevBookmark = _currentBookmark; } } public static void Navigate(string bookmark) { try { HtmlPage.Window.Eval("$.historyLoad('" + bookmark + "');"); } catch (System.ArgumentException e) { Debug.WriteLine(_debugMessage); } } #region NavigationRequested public static event NavigationRequestedEventHandler NavigationRequested; static void RaiseNavigationRequested(string oldbookmark, string newbookmark) { if (NavigationRequested != null) { NavigationRequested(null, new NavigationRequestedEventArgs(oldbookmark, newbookmark)); } } #endregion } public delegate void NavigationRequestedEventHandler(object sender, NavigationRequestedEventArgs fe); public class NavigationRequestedEventArgs : EventArgs { public NavigationRequestedEventArgs(string oldbookmark, string newbookmark) { OldBookMark = oldbookmark; NewBookMark = newbookmark; } public string OldBookMark { get; set; } public string NewBookMark { get; set; } } }