<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Srivatsn's Blog : Silverlight</title><link>http://blogs.msdn.com/srivatsn/archive/tags/Silverlight/default.aspx</link><description>Tags: Silverlight</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>IronPython 2.0 Beta 3 is out!</title><link>http://blogs.msdn.com/srivatsn/archive/2008/06/13/ironpython-2-0-beta-3-is-out.aspx</link><pubDate>Sat, 14 Jun 2008 01:58:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8595112</guid><dc:creator>srivatsn</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/srivatsn/comments/8595112.aspx</comments><wfw:commentRss>http://blogs.msdn.com/srivatsn/commentrss.aspx?PostID=8595112</wfw:commentRss><description>&lt;p&gt;The next stop on the path to IronPython 2.0 is Beta 3 and we pushed it out today. You can get the sources and binaries of IronPython &lt;a href="http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=12988"&gt;here&lt;/a&gt;. As Jimmy mentioned in &lt;a href="http://blog.jimmy.schementi.com/2008/06/dynamic-languages-in-silverlight-2-beta.html"&gt;his release notes&lt;/a&gt; for Dynamic Silverlight last week, this release also contains the sources and binaries of IronPython and Dynamic Silverlight needed to build Silverlight applications with IronPython. This release builds against &lt;a href="http://www.microsoft.com/silverlight/resources/install.aspx?v=2.0"&gt;Silverlight 2.0 Beta 2&lt;/a&gt; – so make sure that is installed. &lt;/p&gt;  &lt;p&gt;Head over to the &lt;a href="http://www.codeplex.com/IronPython/Wiki/View.aspx?title=v2.0%20Beta%203%20Release%20Notes&amp;amp;referringTitle=Home"&gt;release notes&lt;/a&gt; to find out what has changed since the last release and the list of bugs that have been fixed. Congratulations to the team and the community on the release.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8595112" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/srivatsn/archive/tags/IronPython/default.aspx">IronPython</category><category domain="http://blogs.msdn.com/srivatsn/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Accessing IronPython objects from native Javascript</title><link>http://blogs.msdn.com/srivatsn/archive/2008/05/09/accessing-ironpython-objects-from-native-javascript.aspx</link><pubDate>Fri, 09 May 2008 10:16:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8478042</guid><dc:creator>srivatsn</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/srivatsn/comments/8478042.aspx</comments><wfw:commentRss>http://blogs.msdn.com/srivatsn/commentrss.aspx?PostID=8478042</wfw:commentRss><description>&lt;p&gt;Today I got a question by e-mail from someone who wanted to implement something like this &lt;a href="http://msdn.microsoft.com/en-us/library/a0746166.aspx"&gt;MSDN article&lt;/a&gt; in IronPython. The gist of the article is that there is a property called &lt;em&gt;ObjectForScripting&lt;/em&gt; on a WinForms WebBrowser control. If one sets the property to an object, then that object will be available to javascript that executes in the page through &lt;em&gt;window.external&lt;/em&gt;. The Form needs to add the ComVisible attribute.&lt;/p&gt;  &lt;p&gt;.NET attributes cannot be added to a class from IronPython but fortunately objects created are ComVisible by default. So we can try a direct translation of the code in the article :&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;import &lt;/span&gt;clr
clr.AddReference(&lt;span style="color: maroon"&gt;&amp;quot;System.Windows.Forms&amp;quot;&lt;/span&gt;)
&lt;span style="color: blue"&gt;from &lt;/span&gt;System.Windows.Forms &lt;span style="color: blue"&gt;import &lt;/span&gt;Application, Form, WebBrowser, MessageBox

&lt;span style="color: blue"&gt;class &lt;/span&gt;MyForm(Form):
    &lt;span style="color: blue"&gt;def &lt;/span&gt;__init__(self):
        self.wb = WebBrowser()
        self.Controls.Add(self.wb)
        self.wb.ObjectForScripting = self
        self.wb.DocumentText = &lt;span style="color: maroon"&gt;&amp;quot;&amp;quot;&amp;quot;
                               &lt;/span&gt;&lt;font color="#800000"&gt;&amp;lt;html&amp;gt;
                                &amp;lt;body&amp;gt;
                                 &amp;lt;button onclick=&amp;quot;window.external.Test('hello')&amp;quot;&amp;gt;
                                   call client code &lt;span style="color: blue"&gt;&lt;font color="#800000"&gt;from&lt;/font&gt; &lt;/span&gt;script code
                                 &amp;lt;/button&amp;gt;
                                &amp;lt;/body&amp;gt;
                               &amp;lt;/html&amp;gt;&lt;/font&gt;
                               &lt;span style="color: maroon"&gt;&amp;quot;&amp;quot;&amp;quot;
    &lt;/span&gt;&lt;span style="color: blue"&gt;def &lt;/span&gt;Test(self, msg):
        MessageBox.Show(msg)
    
        
Application.Run(MyForm())&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately this doesn't work because ObjectForScripting is being set to an instance of the python type MyForm. In the CLR-world this type will be generated dynamically and will be something like IronPython.NewTypes.System.Windows.Form_1$0 and will not contain any of the python methods. You can verify this by doing &lt;em&gt;clr.GetClrType(MyForm).GetMembers()&lt;/em&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;. The easiest solution then is to define an interface in C# with the methods that you want on your type and implement that interface on your type. So define a simple interface like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IWebBrowserInterop
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;void &lt;/span&gt;Test(&lt;span style="color: blue"&gt;string &lt;/span&gt;message);
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;and in the python code make your class implement that interface:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;from &lt;/span&gt;ClassLibrary1 &lt;span style="color: blue"&gt;import &lt;/span&gt;IWebBrowserInterop
&lt;font color="#0000ff"&gt;&lt;/font&gt;
&lt;span style="color: blue"&gt;class &lt;/span&gt;MyForm(Form, IWebBrowserInterop):
...&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now if you fire up the app and click on the button the MessageBox will popup. You just reached across from unmanaged javascript through COM/.NET to python&lt;/p&gt;

&lt;p&gt;Caveat(s):&amp;#160; This is not type-safe. The IronPython runtime caches the types that are generated. So if there is another class in the python code that is &amp;quot;similar&amp;quot; (same base classes/interfaces and not different __slots__-wise) then this type can be reused. So if you are not bound by other factors, you should try to use Managed JScript instead and the hosting APIs to interact with the python objects.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8478042" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/srivatsn/archive/tags/IronPython/default.aspx">IronPython</category><category domain="http://blogs.msdn.com/srivatsn/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>DeepZoom in dynamic languages</title><link>http://blogs.msdn.com/srivatsn/archive/2008/03/17/deepzoom-in-dynamic-languages.aspx</link><pubDate>Tue, 18 Mar 2008 01:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8296681</guid><dc:creator>srivatsn</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/srivatsn/comments/8296681.aspx</comments><wfw:commentRss>http://blogs.msdn.com/srivatsn/commentrss.aspx?PostID=8296681</wfw:commentRss><description>&lt;P&gt;There's been a lot of talk lately about the DeepZoom technology in Silverlight 2. If you have seen it yet you should definitely goto HardRock's &lt;A href="http://memorabilia.hardrock.com/" mce_href="http://memorabilia.hardrock.com/"&gt;&lt;FONT color=#0000ff&gt;memorabilia&lt;/FONT&gt;&lt;/A&gt; site. That is a 2 billion pixels image that you see in that 640X480 segment there. Instead of getting all those pixels down the wire and killing your system, you only get the pixels needed to display the zoom level you are in. To me one of the reasons this is cool is because it introduces a new paradigm of photo sharing. You can have a long shot picture that captures the essence of your album and then have smaller pictures embedded at the right places which can be zoomed into. I wanted to do that for some photos I'd taken when I went rafting with some friends in the Himalayas some time back. &lt;A href="http://wotc.members.winisp.net/SLDeepZoom/index.html" mce_href="http://wotc.members.winisp.net/SLDeepZoom/index.html"&gt;&lt;FONT color=#0000ff&gt;Here&lt;/FONT&gt;&lt;/A&gt; is the picture of the camp and other pictures embedded in approximate positions of where they were. (Ok I know it sucks, I pretend to have some artistic sense whereas I don't but in my defense I didn't know about deepzoom then or I'd have taken a good long range shot) &lt;/P&gt;
&lt;P&gt;So how do I go about doing this? First thing is obviously you need Silverlight. Then if you are going to use a dynamic language (and there is no reason to use anything else) then you need to download &lt;A href="http://dynamicsilverlight.net/" mce_href="http://dynamicsilverlight.net/"&gt;&lt;FONT color=#0000ff&gt;dynamic silverlight&lt;/FONT&gt;&lt;/A&gt;. John Lam has &lt;A href="http://www.iunknown.com/2008/03/dynamic-silverl.html" mce_href="http://www.iunknown.com/2008/03/dynamic-silverl.html"&gt;&lt;FONT color=#0000ff&gt;a series&lt;/FONT&gt;&lt;/A&gt; &lt;A href="http://www.iunknown.com/2008/03/dynamic-silve-1.html" mce_href="http://www.iunknown.com/2008/03/dynamic-silve-1.html"&gt;&lt;FONT color=#0000ff&gt;of excellent posts&lt;/FONT&gt;&lt;/A&gt; that introduce dynamic silverlight. To create the deepzoom images, you would need &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=457B17B7-52BF-4BDA-87A3-FA8A4673F8BF&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyID=457B17B7-52BF-4BDA-87A3-FA8A4673F8BF&amp;amp;displaylang=en"&gt;&lt;FONT color=#0000ff&gt;DeepZoom composer&lt;/FONT&gt;&lt;/A&gt;. The DeepZoom composer is fairly straightforward. Import the pics you want in your project and drop on to the composer. Make sure that you zoom into the main image and drop the embedded images with the right size. Once your images are ready all you need is a simple app.xaml like so: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Canvas &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;x&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;:&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Class&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="System.Windows.Controls.Canvas"
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://schemas.microsoft.com/client/2007"
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;:&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;x&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;x&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;:&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="parentCanvas"
    &amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;StackPanel&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;MultiScaleImage &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;ViewportWidth&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="1.0" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;x&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;:&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="msi" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Source&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="raftingcamp/info.bin"/&amp;gt;
    &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;StackPanel&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Canvas&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Now you need some simple python code that hooks the mouse click/wheel event handlers and zooms in and out appropriately. Scott Hanselman has an &lt;A href="http://www.hanselman.com/blog/TheWeeklySourceCode18DeepZoomSeadragonSilverlight2MultiScaleImageMouseWheelZoomingAndPanningEdition.aspx" mce_href="http://www.hanselman.com/blog/TheWeeklySourceCode18DeepZoomSeadragonSilverlight2MultiScaleImageMouseWheelZoomingAndPanningEdition.aspx"&gt;&lt;FONT color=#0000ff&gt;excellent sample of that in C#.&lt;/FONT&gt;&lt;/A&gt; I just wrote python code that does the same thing (ofcourse its python so its simpler and more elegant :)). In the constructor, you need to load the xaml and hook up the events: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class &lt;/SPAN&gt;DeepZoomScene(object):
    
    &lt;SPAN style="COLOR: blue"&gt;def &lt;/SPAN&gt;__init__(self):
        self.scene = Application.Current.LoadRootVisual(Canvas(), &lt;SPAN style="COLOR: maroon"&gt;"app.xaml"&lt;/SPAN&gt;)

        self.scene.msi.MouseLeftButtonDown += self.mouseButtonDown
        self.scene.msi.MouseLeftButtonUp += self.mouseButtonUp
        self.scene.msi.MouseMove += self.mouseMove
        self.scene.msi.MouseEnter += self.mouseEnter
        self.scene.msi.MouseLeave += self.mouseLeave

        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;HtmlPage.IsEnabled:
            HtmlPage.Window.AttachEvent(&lt;SPAN style="COLOR: maroon"&gt;"DOMMouseScroll"&lt;/SPAN&gt;, EventHandler[HtmlEventArgs](self.mouseWheel))
            HtmlPage.Window.AttachEvent(&lt;SPAN style="COLOR: maroon"&gt;"onmousewheel"&lt;/SPAN&gt;, EventHandler[HtmlEventArgs](self.mouseWheel))
            HtmlPage.Document.AttachEvent(&lt;SPAN style="COLOR: maroon"&gt;"onmousewheel"&lt;/SPAN&gt;, EventHandler[HtmlEventArgs](self.mouseWheel))&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now its just a matter of writing the event handlers. For example, here is the scroll wheel handler (thanks to Pete Blois for his C# sample). Notice how using a dynamic language massively reduces the lines of code needed. &lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;def &lt;/SPAN&gt;mouseWheel(self, sender, args):
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;self.IsMouseOver:
        delta = args.EventObject.GetProperty(&lt;SPAN style="COLOR: maroon"&gt;"wheelDelta"&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;HtmlPage.Window.GetProperty(&lt;SPAN style="COLOR: maroon"&gt;"opera"&lt;/SPAN&gt;) != None:
            delta = -delta
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;delta == None:
            delta = -(args.EventObject.GetProperty(&lt;SPAN style="COLOR: maroon"&gt;"detail"&lt;/SPAN&gt;))/3
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;HtmlPage.BrowserInformation.UserAgent.IndexOf(&lt;SPAN style="COLOR: maroon"&gt;"Macintosh"&lt;/SPAN&gt;) != -1:
                delta = delta*3

        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;delta &amp;gt; 0:
            self.zoom(1.1, self.lastMousePos)
        &lt;SPAN style="COLOR: blue"&gt;else&lt;/SPAN&gt;:
            self.zoom(0.9, self.lastMousePos)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;(The entire python file is attached). After this, ideally all I'd have to do should be run chiron and create the xap. But there seems to be a bug in Silverlight beta1 wherein if I have the deepzoom files in both the xap then it refuses to read the read the file (this doesnt happen for normal jpgs - only with MultiScaleImage). So I xap'ed just the app folder by doing chiron /d:app /z:app.xap and then keeping my deepzoom folder along with the xap. &lt;/P&gt;
&lt;P&gt;Note: The code is provided "as is" and all that - aka don't blame if it blows up.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8296681" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/srivatsn/attachment/8296681.ashx" length="2733" type="text/plain" /><category domain="http://blogs.msdn.com/srivatsn/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/srivatsn/archive/tags/IronPython/default.aspx">IronPython</category><category domain="http://blogs.msdn.com/srivatsn/archive/tags/Silverlight/default.aspx">Silverlight</category></item></channel></rss>