Page view counter

Did You Know... You Can Create A Silverlight Streaming Application in Minutes?

Three are many details to creating a Silverlight Streaming application, but in this post I'm going to try to boil them down to a recipe for fast success. 

1. Create an account for yourself on the Silverlight Wiidows Live (alpaha) site. 4GB hosting is free, but read the fine print at the bottom of the page.

Once you are set, ignore that for a while and create a simple Silverlight application that does something (e.g., drag and drop).  If you don't want to bother, just steal mine. 

First Get It Working As A Normal Silverlight Control

Scene.xaml

<Canvas xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Ellipse x:Name="myEllipse"
            Canvas.Left="10"
            Canvas.Top ="150"
             Height="100"
             Width="100"
             Fill="Red"
             Stroke="Black"
             StrokeThickness="3" />
<Rectangle x:Name="mySquare"
               Height="100"
               Canvas.Left="150"
               Canvas.Top="150"
               Width="100"
               Fill="Blue"
               Stroke="Black"
               StrokeThickness="3" />
</Canvas>

 

Scene.xaml.js

if (!window.Streaming)
window.Streaming = {};

Streaming.Scene = function() 
{
}

Streaming.Scene.prototype =
{
handleLoad: function(plugIn, userContext, rootElement) 
{
this.plugIn = plugIn;
this.beginX;
        this.beginY;
        this.trackingMouseMove = false;


this.ellipse = this.plugIn.content.FindName("myEllipse");
this.rect = this.plugIn.content.FindName("mySquare");

this.ellipse.addEventListener("MouseLeftButtonDown", 
    Silverlight.createDelegate(this, this.handleMouseDown));
this.ellipse.addEventListener("MouseLeftButtonUp", 
    Silverlight.createDelegate(this, this.handleMouseUp));
this.ellipse.addEventListener("MouseMove", 
    Silverlight.createDelegate(this, this.handleMouseMove));

this.rect.addEventListener("MouseLeftButtonDown", 
    Silverlight.createDelegate(this, this.handleMouseDown));
this.rect.addEventListener("MouseLeftButtonUp", 
    Silverlight.createDelegate(this, this.handleMouseUp));
this.rect.addEventListener("MouseMove", 
    Silverlight.createDelegate(this, this.handleMouseMove));

},


handleMouseDown: function(sender, mouseEventArgs) 
{
   this.beginX = mouseEventArgs.getPosition(null).x;
   this.beginY = mouseEventArgs.getPosition(null).y;
   this.trackingMouseMove = true;
   sender.captureMouse();
},

handleMouseUp: function(sender, eventArgs) 
{
   this.trackingMouseMove = false;
   sender.releaseMouseCapture();
},

handleMouseMove: function(sender, mouseEventArgs) 
{
   if ( this.trackingMouseMove == true )
   {
      var currentX = mouseEventArgs.getPosition(null).x;
      var currentY = mouseEventArgs.getPosition(null).y;
      sender["Canvas.Left"] += currentX - this.beginX;
      sender["Canvas.Top"] += currentY - this.beginY;
      this.beginX = currentX;
      this.beginY = currentY;
   }
}
}

Make It A Streaming Control

Once your code is working and debugged, here are the steps to move it to a streaming application:

  1. Change the event-handling wire-up from the .js file to the XAML file. [I believe this is a bug, and I've filed a query about it] .

Scene.xaml

<Canvas xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Ellipse x:Name="myEllipse"
             Canvas.Left="10"
             Canvas.Top="150"
             Height="100"
             Width="100"
             Fill="Red"
             Stroke="Black"
             StrokeThickness="3"
             MouseLeftButtonDown="onMouseDown"
             MouseLeftButtonUp="onMouseUp"
             MouseMove="onMouseMove" />
    <Rectangle x:Name="mySquare"
               Height="100"
               Canvas.Left="150"
               Canvas.Top="150"
               Width="100"
               Fill="Blue"
               Stroke="Black"
               StrokeThickness="3"
               MouseLeftButtonDown="onMouseDown"
               MouseLeftButtonUp="onMouseUp"
               MouseMove="onMouseMove" />
</Canvas>


Scene.xaml.js

Streaming.Scene.prototype =
{
handleLoad: function(plugIn, userContext, rootElement) 
{
}
}

var beginX;
var beginY;
var trackingMouseMove = false;


function onMouseDown(sender, mouseEventArgs)
{
   beginX = mouseEventArgs.getPosition(null).x;
   beginY = mouseEventArgs.getPosition(null).y;
   trackingMouseMove = true;
   sender.captureMouse();
}


function onMouseUp(sender, mouseEventArgs)
{
   trackingMouseMove = false;
   sender.releaseMouseCapture();
}

function onMouseMove(sender, mouseEventArgs)
{
   if ( trackingMouseMove == true )
   {
      var currentX = mouseEventArgs.getPosition(null).x;
      var currentY = mouseEventArgs.getPosition(null).y;
      sender["Canvas.Left"] += currentX - beginX;
      sender["Canvas.Top"] += currentY - beginY;
      beginX = currentX;
      beginY = currentY;
   }
}

 

2. Change the Silverligh.js script statement in Default.html

<script type="text/javascript" src="http://agappdom.net/h/silverlight.js"></script>

3. Modify the div in Default.html adding a varaible for the parent id (that points to the div that holds the script, and change the first letter of createSilverlight to uppercase (we'll be adding that method in a moment)

<div id="mySilverlightControlHost">
<script type="text/javascript">
    var parentElement = document.getElementById("mySilverlightControlHost");
    CreateSilverlight();
</script>
</div>

 

4. Replace the function createSilverlight() with the new function CreateSilverlight()

function CreateSilverlight()
{
     Silverlight.createHostedObjectEx(
     {
        source: "streaming:/[AccountID]/[ApplicationName]",
        parentElement: document.getElementById(["mySilverlightControlHost"])
     });
}
 
There are three elements here to replace (I've placed them in square brackets)
  • AccountID - Assigned to you when you receive your Streaming Account (see picture below)
  • The Application Name
  • The ID of the Div (in the example shown above, mySilverlightControlHost

AccountID

     5. Create a Manifest File that describes the app. Note that you need to designate the xaml file, the method to call on load, and the js files.It must be called Manifest.xml

    AddManifest 
    <?xml version="1.0" encoding="utf-8" ?>
    <SilverlightApp>
        <source>Scene.xaml</source>
        <onLoad>CreateSilverlight()</onLoad>
        <version>1.0</version>
        <width>600</width>
        <height>400</height>
        <jsOrder>
            <js>http://agappdom.net/h/silverlight.js</js>
            <js>Default.html.js</js>
            <js>Scene.xaml.js</js>
        </jsOrder>
    </SilverlightApp>
    
    

     

    6. Right click on the Solution and click on "Open Folder in Explorer" In the File explorer select Scene.xaml, Scene.xaml.js and Manifest.xml and right click to create a zip file, which will allow you to craete a zip file with the name of your application, which is just what you want.

    ZippingTheManifest

    7. Return to the streaming application (remember the streaming application?)  and click on Manage Applications

    ManageApps

     

    8. Click on Upload a Silverlight Application  and fill in the Application Name and then click on the Browse button to find the zip file you created. Once the name of the Zip file is in the window, click upload.

     

    UploadApp

     

    9. Once the application is uploaded you can quickly test that it is working by clicking on Launch Application Test Page

    Launch  

     

    10 Assuming your Streaming Silverlight application works fine in the test page, there are now two ways for you to deploy. The Manage Applications page that you are returned to provides you with three explicit steps and the needed code to add your application to any Web Page

     

    AddToWebPage

     

    Finally, you can also add the streaming application to your page by using an IFrame,

     

    <iframe src=http://silverlight.services.live.com/invoke/20712/StreamingDragAndDrop/iframe.html 
    frameborder="1" width="400" scrolling="no" height="180"></iframe>

    You can drop this IFrame into virtually any web page or blog and it "just works" 

    We are working on making it work in this blog, in fact, but for now, you can see it working here
    Technorati Profile  
     
    Published 27 January 2008 10:00 AM by jesseliberty

    Comments

    # BenHayat said on 27 January, 2008 12:08 PM

    Jesse, why do you use such small fonts, once you start with the code sections. It seems like it gets smaller and smaller as towards the end of document. I can't even read it with my glasses on. For example if you look at ScottGu blogs, the codes are nice and crisp. Here you're mixing the same fonts and colors for code and your text. It's happening in almost all of your blogs. Could you please look into it?

    Thank you very much!

    # jesseliberty said on 27 January, 2008 12:38 PM

    >>Jesse, why do you use such small fonts,... Here you're mixing the same fonts and colors for code and your text.  Could you please look into it?<<

    Sure, I apologize; this is an artifact of the different LiveWriter plug-ins I've tried. I'll fix it up here and going forward. Thanksf for the valuable feedback, I'm sure its driving lots of people nuts.

    # BenHayat said on 27 January, 2008 01:01 PM

    >>Thanksf for the valuable feedback, I'm sure its driving lots of people nuts.<<

    Jesse, you put so much efforts and passion into these blogs that I feel, it's ashame that someone walks away from it.

    Up to now, every book or article, seem to go over all these critical points (i.e. different animations, how to deploy, what are the default  width and height and etc.), as if everyone knows them already, but you have are zooming and giving details about each. I hope in the future, you have time to do the same for version 2.0 with all those new features. Thanks for your help!

    # jesseliberty said on 27 January, 2008 01:12 PM

    Ben, You are way too kind (and my boss's email is simonmu@microsoft.com) <smile>.

    YES, the goal is to do EXACTLY this for Silverlight 2.0, which is much bigger and needs it all the more.

    By the way, take a look at the article and tell me if this is closer to what you had in mind.

    # BenHayat said on 27 January, 2008 02:17 PM

    >>Ben, You are way too kind<<

    Jesse, I think by now, you know that I speak my mind, to make things better. Sometime people take it the wrong way, but the intention is always to improve.

    >>and my boss's email is simonmu@microsoft.com<<

    If he is in a caliber to be "Your" boss, then he doesn't need to reminded, and if he needs to be reminded, then he is in wrong position ;-)

    >>By the way, take a look at the article and tell me if this is closer to what you had in mind.<<

    YES much, much better. Jesse, if you have time, the previous (which is a very valuable as well) should be fixed.

    Thanks for prompt response!

    # wisecarver said on 27 January, 2008 06:10 PM

    Thanks Jesse, another awesome article.

    # DotNetKicks.com said on 27 January, 2008 06:12 PM

    You've been kicked (a good thing) - Trackback from DotNetKicks.com

    # jesseliberty said on 27 January, 2008 08:09 PM

    >>YES much, much better. Jesse, if you have time, the previous (which is a very valuable as well) should be fixed.<<

    Ben, actually, this one is too large (it is set to 3 when the standard is 2). I know that the 2 can be a little hard on our middle-aged eyes, but at 3 the entire article becomes huge and difficult for folks to manage.

    Fortunately, there is a solution; IE will happily enlarge it for you, in the lower right hand corner is a magnifier; setting it to 125% works quite well :-)

    I'll look at other fonts to see what I can find to make it look as crisp as Scott's.

    Thanks.

    # jesseliberty said on 27 January, 2008 08:22 PM

    Ben, you are a genius; I owe you a huge favor. I went and finally *looked* at Scott's blog. The answer is that his text is the same size as my (smaller) text; but his code is not in line; it is all screen shots; and you are right, it is much easier to read. Sold!

    I'll fix the previous post by tomorrow evening; sooner if I can get my machine to cooperate.

    Thanks!

    -j

    # jesseliberty said on 27 January, 2008 08:45 PM

    Pictures for code. Done: http://tinyurl.com/2wehf3

    # o UAU nosso de cada dia said on 28 January, 2008 04:07 AM

    Que tal 4G gratuitos pra hospedar tuas aplicações em Silverlight? Well... eu sou suspeito pra falar,

    # Noticias externas said on 28 January, 2008 04:25 AM

    Que tal 4G gratuitos pra hospedar tuas aplicações em Silverlight? Well... eu sou suspeito pra falar,

    # MSDN Blog Postings » 4G para hospedar tuas aplica????es Silverlight... de gra??a :) said on 28 January, 2008 04:40 AM

    Pingback from  MSDN Blog Postings  &raquo; 4G para hospedar tuas aplica????es Silverlight... de gra??a  :)

    # » Daily Bits - January 28, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats. said on 28 January, 2008 08:33 AM

    Pingback from  &raquo; Daily Bits - January 28, 2008 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.

    # BenHayat said on 28 January, 2008 09:45 AM

    >>Pictures for code. Done: tinyurl.com/2wehf3<<

    Oh ya, now we are talking. Great work!

    Middle age??? ;-)

    # karlshifflett said on 28 January, 2008 11:46 AM

    Jesse,

    Great post sir!

    I've just posted an article on Code Project showing folks how to create a screen capture video that is compatible with free Silverlight Streaming's 300KB max bitrate.

    www.codeproject.com/.../HowToCreateArticleVideos.aspx

    I'll update my article tonight and tell readers about this article.

    Cheers,

    Karl

    # BenHayat said on 28 January, 2008 11:56 AM

    Karl;

    I just did quick look at your article (will read it tonight) and it looks very nice. Thank you for your contributions!

    ..Ben

    # Community Blogs said on 28 January, 2008 04:52 PM

    Pete Brown shows a way to add drop shadows to objects in Expression Design and staying 100% vector, and

    # Bryant Likes's Blog said on 28 January, 2008 07:13 PM

    Saw that Jesse Liberty ( via WynApse ) has a nice post on creating Silverlight streaming applications