Archive for March, 2008

As I’ve been migrating towards using C# with Silverlight 2, the best way I’ve found to learn it (aside from the tutorials/labs out there), is to actually write applications. Today, I wrote one that emulates an electronic drum kit.

The idea was to create a set of drum “pads”, associate each pad with a key on the keyboard, and then play an associated MediaElement sound when the key is pressed. The end result looks like this:

You can check out the live version of it here.

The application was created by using a single “drum pad” that has a storyboard on it that will flash the pad red when a key is pressed. The pad was instantiated 9 times and positioned as shown in the screenshot.

Interestingly enough, the most challenging part of creating the app was getting the sound to play. The “press a key, play the sound” idea sounds really simple, and works great.

Once.

After that, the sound changes volume randomly, and does not play consistently. Searching the Silverlight.net forums turned up a history on this issue back as far as June of 2007. Unfortunately, this problem was in Silverlight 1, and it’s still present in Silverlight 2.

I developed a work-around that is probably not ideal, but it does the job.

What I discovered is that each time a MediaElement is triggered, it will play fine one time. So my answer was to dynamically create a new MediaElement each time a key is pressed. The problem with this approach is cleanup. Once 125 or so are added, the application crashes.

Attaching a MediaEnded event to remove the MediaElement when the audio is done playing results in choppy or truncated audio. Instead, I elected to place the dynamically created MediaElements into a specific container on the main canvas, and use MediaEnded to check the number of children in that container.

When a preset threshold has been reached (the default is 25), I run a quick loop that deletes all the children on that canvas, effectively clearing out my cache of “used” MediaElements. This allows my sounds to play each time a key is pressed, and cleans itself up.

Codewise, my MediaElement is defined globally in the Page class as follows:
MediaElement _mediaElement;

When a key is pressed, I play the storyboard for the appropriate instance of the drum pad, instantiate the MediaElement that will hold the sound for that keypress, assign the sound to it, then call a function called “do_MediaElement” that handles tasks common to all key presses.

These steps look like this:

case Key.J:
crashPad.padHit.Begin();
_mediaElement = new MediaElement();
_mediaElement.Source = new Uri(crashDrumSound, UriKind.Relative);
do_MediaElement();
break;

The do_MediaElement function attaches a MediaEnded event to the newly created MediaElement, then adds it to the media element container. It then plays the sound.

void do_MediaElement()
{
_mediaElement.MediaEnded += new RoutedEventHandler(_mediaElement_MediaEnded);
mediaElementContainer.Children.Add(_mediaElement);
_mediaElement.Play();
}

Earlier I mentioned that I didn’t have luck using a MediaEnded event to remove the MediaElement when the sound finishes playing. Instead, it’s used to do a quick check on the number of children in the container I’m using. If the number of children is greater than or equal to the defined threshold, then it does a quick removal of the children on that canvas.

Note that the line that removes the children removes them from the bottom up - always removing the child element at index 0. This is because you can’t just directly count through the children as they are being removed without running into problems - the first time through there are 25 children, then 24, then 23, and so on. If I attempted to remove the node at position 25, it would fail since node 25 doesn’t exist after any node is removed. The loop has to compensate for the fact that it is causing the number of children to reduce each time through.

void _mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
int count = mediaElementContainer.Children.Count;

if (count >= cleaningThreshold)
{
for (int i = 0; i < cleaningThreshold; i++)
{
mediaElementContainer.Children.RemoveAt(0);
}
}
}

I have some ideas for expanding upon the app and may come back to it at some point in the future. For the time being though, it does what I set out to do, and my son seems to enjoy it quite a bit.

I’ve had some time to experiment more with DeepZoom, and was able to provide the Expression Composer team with one of the projects that was causing me problems.

If you nest enough images, the images become “small” enough that the encoder doesn’t know what to do (and in my case, did nothing). I have found that this seems to occur for me once I get to about 4 images or so, but only if they are overlapping. If I scale the images and position them next to one another, Composer will export the files as expected.

This led to a different problem, however. When placing the images next to one another so that each is smaller than the previous one, by the time I’m zoomed in on the last of 9 images, it’s blurry and illegible. This one seems to be more of an issue with the rendering engine, because the JPG the multiscale image control is using looks great. It *looks* like you’re zoomed way in on an image, but it’s not resolving to the smooth version.

These kinds of issues are to be expected with pre-release software, of course. I’m really excited about this feature though. There’s a ton of possibility there - I’m looking forward to updates as they become available!

Like everyone else, I was deeply interested in the DeepZoom feature that introduced at MIX.

I spent some time playing with it, and at the moment, it looks to have some limitations. Granted, it’s a preview release (Expression Composer), and I’m probably not doing the software any favors by choking it up with a half-dozen or so high-res photos, but it seems really hit or miss. I can lay out all of my photos in the main display, export it, and it works fine.

When I start scaling the images down and positioning them in order to create the “zoom” effect, once I get past 3 or 4 images, the software just stops updating the output file. It *says* it’s exporting, but it doesn’t actually seem to *do* anything.

Despite what you say, Dave, I refuse to believe I’m the *only* one! =)

The project described in my last post described how to set up a basic object-oriented approach to Silverlight in JavaScript. Now we need a way to access properties on the object, so we’re going to add “getters” and “setters”.

If you don’t have the last project, it’s available here.

Getters and setters are just functions (or methods) added to the prototype object, which is located in the objSquare.js file. Getters “return” values to the calling code, while setters set some value on the specified object.

Let’s add a getter that will return the canvas top of the specified object.

In the objSquare.js file, add a comma after the closing curly brace of the “initialize” function, then add the following code:

get_top : function()
{
return this._square["Canvas.Top"];
}

This getter is called by specifying the object name, a dot (.), then the method - get_top(). Because it specifies a return value, you can assign it to a variable or use it for calculations.

Let’s test this out. Open the Page.xaml.js file, and after the lines of code that instantiate the two square objects, add a line to open a message box with the value of the canvas top for “newSquare”.

alert(newSquare.get_top());

Run the app and you should get a message box with “100″ in it. Change “newSquare” to “newSquare1″ and run it again. You should see a message box with “500″ in it.

Setters work the same way, but they do not return a value. Instead, they perform an action on the specified object.

Add a comma after the closing curly brace for the get_top method, and add a method called “set_top” that looks like this:

set_top : function(newTop)
{
this._square["Canvas.Top"] = newTop;
}

“newTop” is a value we will pass into the setter, which will then be applied to the specified object.

Save the objSquare.js file and go back to Page.xaml.js. Add the following two lines of code after the alert:

newSquare.set_top(250);
newSquare1.set_top(50);

This calls the setter for both of the square objects in the app. The first one will be moved so that the canvas top is at 250, and the second will be moved so the canvas top is 50. Run the program - you should see the message box open, then both squares draw at the specified location.

If you’ve made a habit of using a traditional JavaScript coding style, you can probably see where this is a useful, powerful way of creating/manipulating objects for Silverlight. It offers another benefit too, which you may have already guessed: “custom” properties.

I’ve seen people ask on the forums about accessing properties that don’t exist by default - for example, canvas right.

Let’s add a getter that will return a right canvas value on a specified object.

In the objSquare.js file, add a comma after the closing curly brace for the set_top method, then add the following getter:

get_right : function()
{
return this._square["Canvas.Left"] + this._square.width;
}

As you can probably figure out from reading the code, this will return the specificed objects canvas left value added to the specified object’s width, which will give us the object’s right bound. It’s not really adding a property to the object per se, but it is cutting down on potential code clutter by setting up the calculation in a single place.

Save the objSquare.js file, and open Page.xaml.js. Add a message box that calls the get_right method on the newSquare object:

alert(newSquare.get_right());

When the program is run, you should see a message box open that displays the top value for “newSquare1″ (500), then the squares will draw on the canvas, and a second message box will open that displays “125″, which is the right canvas value for the “newSquare” object.

The final files for this entry can be found here.

NOTE: The project files included for this entry use the Silverlight 2 runtime, so you will need to have that installed on your system. The example code itself is still in JavaScript.

I’ve wanted to move towards programming in C# for a while, and it seems like Silverlight 2 is giving me a great reason to make the time to learn it. In the meantime, in preparation for working with objected-oriented programming (it’s been a while since I did C++), I’ve started trying to shift my JavaScript habits towards an OO format in order to ease my transition.

The problem? The lack of a simple tutorial to bridge between “ad hoc” functions and a more object oriented approach. Chris Klug was a big help in shedding some light on the subject for me, so I owe him a big “thank you”.

So here it is, a simple example.

Start Blend and create a new Silerlight 1 project called squareObject. Change the name of the default canvas to “rootCanvas” and set the dimensions to 800×600. Add another canvas called “content”. Don’t worry about the width/height of the content canvas - we’ll be taking care of that programmatically. Save the project.

First, we need to create a “constructor” for the square object class that we’re going to write. Create a new JS file in the project folder called objSquare.js, and type in the code for the constructor. The constructor function looks like this (don’t worry if this part doesn’t make sense yet - when you see the whole context, it will become clearer):

square = function(name, Parent, height, width, left, top)
{
    this.initialize(name, Parent, height, width, left, top);
}

This is a constructor that will call the initialize function in the object class we’re about to code. We’re going to pass in a name for the object instance, the object parent, a height, width, left, and top position, so we are creating buckets to hold all of those values.

We want to continue on by creating the object class, which looks like this:

square.prototype =
{
    initialize: function(name, Parent, height, width, left, top)
    {
 var xaml =  ‘<Rectangle Name=”‘ + name + ‘” Canvas.Left=”‘ + left + ‘” Canvas.Top=”‘ + top + ‘” Width=”‘ + width + ‘” Height=”‘ + height + ‘” Fill=”#FFCC0000″/>’;

        this._parent = Parent;       
        this._host = this._parent.getHost();
        this._square = this._host.content.createFromXaml(xaml);
        this._parent.children.add(this._square);
    }
 }

The class defines a function called initialize, which is called by the constructor that was created in the prior step. The initialize function creates a new rectangle object using the name, height, width, left, and top properties that were passed in, and then adds it to the “content” canvas, which is located using the “Parent” value that was passed.

Save the objSquare.js file.

This would be a good time to add a reference to this script file in the default.html page, so open that file up, and add the following script reference to the header section of the page:

<script type=”text/javascript” src=”objSquare.js” mce_src=”objSquare.js”></script>

While you’re in there, change the width and height styles to 800×600 as well so that they match the size of our root canvas.

Save the default.html file.

Now we want to make some edits to the Page.xaml.js file in order to make use of our square class when the application is loaded. Open the Page.xaml.js file. There’s some sample code in there that can be removed. The skeleton of the Page.xaml.js file should look like this:

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

squareObject.Page = function()
{
}

squareObject.Page.prototype =
{
 handleLoad: function(control, userContext, rootElement)
 {
  this.control = control;

 }
}

The handleLoad function is called when Silverlight creates an instance of the “squareObject” app. Notice that control, userContext, and rootElement are all passed into the handleLoad function. Under the line that says “this.control = control;”, we’re going to add a few lines of code in order to set up references to the rootCanvas and content canvas objects. We also want to set the content cavas width and height to match the root canvas. To do that, this is the code that is used:

this.rootElement = rootElement;
this.content = rootElement.findName(”content”);
      
this.content.width = this.rootElement.width;
this.content.height = this.rootElement.height;

At this point, we’re ready to add code to call the constructor and have it create an object on the canvas for us, so let’s quickly review what we’ve done. We just finished modifying the Page.xaml.js file to create some references to the rootElement and content canvas, and made the content canvas resize to the size of the rootElement.

Before that, we created a square class in objSquare.js. In our square class, we added a constructor function which when called, will create an instance of the “square” object that is defined in the square.prototype class. We will now add some code to the Page.xaml.js file to create an object on the canvas.

Beneath the code you just added to the file, add the following code:

var newSquare = new square(”square1″, rootElement, 25, 25, 100, 100);

This code creates a new object called “newSquare” which is of the object type square. We passed the name “square1″ to be used in the XAML as a unique identifier for the XAML object, the rootElement, which is used to locate the parent object and insert the XAML into the content canvas, and then width, height, left, and right values.

Save the file and run.

You will see a small red square appear on the canvas. By manipulating these numbers, we can change the size or location of the object being created. We can also easily create multiple instances of an object. Add a line of code beneath the one you just added, with the following:

var newSquare1 = new square(”square2″, rootElement, 125, 125, 500, 500);

Run the project again, and you should see two squares.

If you’re looking to start moving towards object oriented programming, take some time and play with this example. I will be revisiting it in additional entries soon to add functionality and custom properties to the square class.

There’s also a good article on OO JavaScript here, though it’s not in the context of Silverlight, it’s still good stuff.

The project files are here if you want to take the easy way out.