<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Jim Zimmerman - ASP.NET, MVC, Ajax and ORM - Silverlight</title>
    <link>http://www.jimzimmerman.com/blog/</link>
    <description>Web Development Jornal</description>
    <language>en-us</language>
    <copyright>Jim Zimmerman</copyright>
    <lastBuildDate>Wed, 30 Apr 2008 07:12:40 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.9.6264.0</generator>
    <managingEditor>jim@jimzimmerman.com</managingEditor>
    <webMaster>jim@jimzimmerman.com</webMaster>
    <item>
      <trackback:ping>http://www.jimzimmerman.com/blog/Trackback.aspx?guid=6db84451-2f0d-492c-b13c-74495a47fdc4</trackback:ping>
      <pingback:server>http://www.jimzimmerman.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jimzimmerman.com/blog/PermaLink,guid,6db84451-2f0d-492c-b13c-74495a47fdc4.aspx</pingback:target>
      <dc:creator>Jim Zimmerman</dc:creator>
      <wfw:comment>http://www.jimzimmerman.com/blog/CommentView,guid,6db84451-2f0d-492c-b13c-74495a47fdc4.aspx</wfw:comment>
      <wfw:commentRss>http://www.jimzimmerman.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6db84451-2f0d-492c-b13c-74495a47fdc4</wfw:commentRss>
      <slash:comments>96</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I had to solve an interesting problem the other day for our new Silverlight 2 chat
app for our site.  People were closing the browser window and not clicking the
leave chat button which clears out their chat session.  So we needed to find
a way to close out their chat session.  There are several ways to solve this
problem, write a windows service to clear out stale sessions among many others, but
the real solution was to figure out a way to have a Silverlight function get called
from JavaScript when they close the browser window.
</p>
        <p>
So the first problem to solve is to catch the browser window close event in JavaScript. 
Here is the the script you need to do just that on all 3 browsers, Firefox, IE and
Safari.
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">script</span>
          <span style="COLOR: rgb(255,0,0)">type</span>
          <span style="COLOR: rgb(0,0,255)">='text/javascript'&gt; </span> window.onbeforeunload
= <span style="COLOR: rgb(0,0,255)">function</span>() { <span style="COLOR: rgb(0,0,255)">var</span> slCtl
= document.getElementById(<span style="COLOR: rgb(163,21,21)">"Xaml1"</span>); slCtl.Content.mySLapp.StopChatting();
alert(<span style="COLOR: rgb(163,21,21)">"You have been logged out"</span>); } <span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(163,21,21)">script</span><span style="COLOR: rgb(0,0,255)">&gt; </span></pre>
        <p>
The Xaml1 control is referencing the asp:Silverlight control that is on the page. 
the slCtl.Content.mySLapp.StopChatting();  call is calling a method on my c#
class in the silverlight project.  In order to make this work you will need to
do two things in your silverlight project.  The first thing you need to do is
to register managed code for client script access.  In the Application_Startup
method in App.xaml.cs, call the RegisterScriptableObject() method on the HtmlPage
object and pass in your type that you want to reference in your script.  Here
is a sample of how to do it.
</p>
        <pre class="code"> </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">using</span> System.Windows.Browser; <span style="COLOR: rgb(0,0,255)">namespace</span> Chat
{ <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,255)">partial</span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(43,145,175)">App</span> : <span style="COLOR: rgb(43,145,175)">Application </span> { <span style="COLOR: rgb(0,0,255)">public</span> App()
{ <span style="COLOR: rgb(0,0,255)">this</span>.Startup += <span style="COLOR: rgb(0,0,255)">this</span>.Application_Startup; <span style="COLOR: rgb(0,0,255)">this</span>.Exit
+= <span style="COLOR: rgb(0,0,255)">this</span>.Application_Exit; <span style="COLOR: rgb(0,0,255)">this</span>.UnhandledException
+= <span style="COLOR: rgb(0,0,255)">this</span>.Application_UnhandledException; InitializeComponent();
} <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">void</span> Application_Startup(<span style="COLOR: rgb(0,0,255)">object</span> sender, <span style="COLOR: rgb(43,145,175)">StartupEventArgs</span> e)
{ <span style="COLOR: rgb(0,128,0)">// Load the main control </span><span style="COLOR: rgb(0,0,255)">this</span>.RootVisual
= <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">Page</span>(); <span style="COLOR: rgb(0,128,0)">//Setup
some scriptable managed types for access from Javascript. </span><span style="COLOR: rgb(43,145,175)">ScriptableType</span> st
= <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">ScriptableType</span>(); <span style="COLOR: rgb(43,145,175)">HtmlPage</span>.RegisterScriptableObject(<span style="COLOR: rgb(163,21,21)">"mySLapp"</span>,
st); } <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">void</span> Application_Exit(<span style="COLOR: rgb(0,0,255)">object</span> sender, <span style="COLOR: rgb(43,145,175)">EventArgs</span> e)
{ } <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">void</span> Application_UnhandledException(<span style="COLOR: rgb(0,0,255)">object</span> sender, <span style="COLOR: rgb(43,145,175)">ApplicationUnhandledExceptionEventArgs</span> e)
{ } } }</pre>
        <pre class="code">Now we need to create the ScriptableType class and add the ScriptableMemberAttribute 
<br />
attribute, which is required in order to expose the members to client script.</pre>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">using</span> System.Windows.Browser; <span style="COLOR: rgb(0,0,255)">namespace</span> Chat
{ <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(43,145,175)">ScriptableType </span> { <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(43,145,175)">ServiceHandler</span> svc;
[<span style="COLOR: rgb(43,145,175)">ScriptableMember</span>()] <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,255)">void</span> StopChatting()
{ svc = <span style="COLOR: rgb(43,145,175)">ServiceHandler</span>.Instance; svc.StopChatting();
} } }<br />
You can also return a type string if you want and then alert that string that is returned
in JavaScript.<br />
It really is a very powerful feature and I am very excited to use this power in many
other ways. Happy coding!</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://www.jimzimmerman.com/blog/aggbug.ashx?id=6db84451-2f0d-492c-b13c-74495a47fdc4" />
      </body>
      <title>JavaScript to Managed Code in Silverlight 2 Beta 1</title>
      <guid isPermaLink="false">http://www.jimzimmerman.com/blog/PermaLink,guid,6db84451-2f0d-492c-b13c-74495a47fdc4.aspx</guid>
      <link>http://www.jimzimmerman.com/blog/2008/04/30/JavaScript+To+Managed+Code+In+Silverlight+2+Beta+1.aspx</link>
      <pubDate>Wed, 30 Apr 2008 07:12:40 GMT</pubDate>
      <description>&lt;p&gt;
I had to solve an interesting problem the other day for our new Silverlight 2 chat
app for our site.&amp;nbsp; People were closing the browser window and not clicking the
leave chat button which clears out their chat session.&amp;nbsp; So we needed to find
a way to close out their chat session.&amp;nbsp; There are several ways to solve this
problem, write a windows service to clear out stale sessions among many others, but
the real solution was to figure out a way to have a Silverlight function get called
from JavaScript when they close the browser window.
&lt;/p&gt;
&lt;p&gt;
So the first problem to solve is to catch the browser window close event in JavaScript.&amp;nbsp;
Here is the the script you need to do just that on all 3 browsers, Firefox, IE and
Safari.
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;script&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;type&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;='text/javascript'&amp;gt; &lt;/span&gt; window.onbeforeunload
= &lt;span style="COLOR: rgb(0,0,255)"&gt;function&lt;/span&gt;() { &lt;span style="COLOR: rgb(0,0,255)"&gt;var&lt;/span&gt; slCtl
= document.getElementById(&lt;span style="COLOR: rgb(163,21,21)"&gt;"Xaml1"&lt;/span&gt;); slCtl.Content.mySLapp.StopChatting();
alert(&lt;span style="COLOR: rgb(163,21,21)"&gt;"You have been logged out"&lt;/span&gt;); } &lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;script&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The Xaml1 control is referencing the asp:Silverlight control that is on the page.&amp;nbsp;
the slCtl.Content.mySLapp.StopChatting();&amp;nbsp; call is calling a method on my c#
class in the silverlight project.&amp;nbsp; In order to make this work you will need to
do two things in your silverlight project.&amp;nbsp; The first thing you need to do is
to register managed code for client script access.&amp;nbsp; In the Application_Startup
method in App.xaml.cs, call the RegisterScriptableObject() method on the HtmlPage
object and pass in your type that you want to reference in your script.&amp;nbsp; Here
is a sample of how to do it.
&lt;/p&gt;
&lt;pre class=code&gt;&amp;nbsp;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;using&lt;/span&gt; System.Windows.Browser; &lt;span style="COLOR: rgb(0,0,255)"&gt;namespace&lt;/span&gt; Chat
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;partial&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;App&lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;Application &lt;/span&gt; { &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; App()
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Startup += &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Application_Startup; &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Exit
+= &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Application_Exit; &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.UnhandledException
+= &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Application_UnhandledException; InitializeComponent();
} &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; Application_Startup(&lt;span style="COLOR: rgb(0,0,255)"&gt;object&lt;/span&gt; sender, &lt;span style="COLOR: rgb(43,145,175)"&gt;StartupEventArgs&lt;/span&gt; e)
{ &lt;span style="COLOR: rgb(0,128,0)"&gt;// Load the main control &lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.RootVisual
= &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Page&lt;/span&gt;(); &lt;span style="COLOR: rgb(0,128,0)"&gt;//Setup
some scriptable managed types for access from Javascript. &lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;ScriptableType&lt;/span&gt; st
= &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;ScriptableType&lt;/span&gt;(); &lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlPage&lt;/span&gt;.RegisterScriptableObject(&lt;span style="COLOR: rgb(163,21,21)"&gt;"mySLapp"&lt;/span&gt;,
st); } &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; Application_Exit(&lt;span style="COLOR: rgb(0,0,255)"&gt;object&lt;/span&gt; sender, &lt;span style="COLOR: rgb(43,145,175)"&gt;EventArgs&lt;/span&gt; e)
{ } &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; Application_UnhandledException(&lt;span style="COLOR: rgb(0,0,255)"&gt;object&lt;/span&gt; sender, &lt;span style="COLOR: rgb(43,145,175)"&gt;ApplicationUnhandledExceptionEventArgs&lt;/span&gt; e)
{ } } }&lt;/pre&gt;
&lt;pre class=code&gt;Now we need to create the ScriptableType class and add the ScriptableMemberAttribute 
&lt;br&gt;
attribute, which is required in order to expose the members to client script.&lt;/pre&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;using&lt;/span&gt; System.Windows.Browser; &lt;span style="COLOR: rgb(0,0,255)"&gt;namespace&lt;/span&gt; Chat
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;ScriptableType &lt;/span&gt; { &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;ServiceHandler&lt;/span&gt; svc;
[&lt;span style="COLOR: rgb(43,145,175)"&gt;ScriptableMember&lt;/span&gt;()] &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; StopChatting()
{ svc = &lt;span style="COLOR: rgb(43,145,175)"&gt;ServiceHandler&lt;/span&gt;.Instance; svc.StopChatting();
} } }&lt;br&gt;
You can also return a type string if you want and then alert that string that is returned
in JavaScript.&lt;br&gt;
It really is a very powerful feature and I am very excited to use this power in many
other ways. Happy coding!&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.jimzimmerman.com/blog/aggbug.ashx?id=6db84451-2f0d-492c-b13c-74495a47fdc4" /&gt;</description>
      <comments>http://www.jimzimmerman.com/blog/CommentView,guid,6db84451-2f0d-492c-b13c-74495a47fdc4.aspx</comments>
      <category>Silverlight</category>
    </item>
  </channel>
</rss>