September 14, 2007

Javascript Event Handler for Silverlight

So, a couple of us at work have been forging ahead with Silverlight and created a nice way to handle mouse click events. It's an idea borrowed from one of the tutorials/quick-starts that you can find on Microsoft's Silverlight site.

Here's the code to set-up an EventHandler javascript class:

// Javascript class that handles mouse events 
if (!window.EventHandler)
	window.EventHandler = {};

EventHandler = function() 
{
}

//Properties
EventHandler.prototype.target = this.target;
EventHandler.prototype.clickHandler = this.clickHandler;
EventHandler.prototype.mouseEnterAnim = this.mouseEnterAnim;
EventHandler.prototype.mouseLeaveAnim = this.mouseLeaveAnim;
EventHandler.prototype.mouseDownAnim = this.mouseDownAnim;
EventHandler.prototype.mouseUpAnim = this.mouseUpAnim;


EventHandler.prototype =
{

    //Property Setters
    setTarget: function(theTarget)
    {
        this.target = theTarget;
    },

    setClickHandler: function(ClickHandler)
    {
        this.clickHandler = ClickHandler;
    },
    
    setMouseEnterAnim: function(MouseEnterAnim)
    {
        this.mouseEnterAnim = MouseEnterAnim;
    },
    
    setMouseLeaveAnim: function(MouseLeaveAnim)
    {
        this.mouseLeaveAnim = MouseLeaveAnim;
    },
    
    setMouseDownAnim: function(MouseDownAnim)
    {
        this.mouseDownAnim = MouseDownAnim;
    },
    
    setMouseUpAnim: function(MouseUpAnim)
    {
        this.mouseUpAnim = MouseUpAnim;
    },

    handleLoad: function() 
	{
	    // Register event handlers
	    this.target.addEventListener("mouseEnter", 
Silverlight.createDelegate(this, this.handleMouseEnter));
	    this.target.addEventListener("mouseLeave", 
Silverlight.createDelegate(this, this.handleMouseLeave));
	    this.target.addEventListener("mouseLeftButtonUp", 
Silverlight.createDelegate(this, this.handleMouseUp));
                   this.target.addEventListener("mouseLeftButtonDown", 
Silverlight.createDelegate(this, this.handleMouseDown));
    },

    handleMouseEnter: function(sender, eventArgs) {
        sender.findName(this.mouseEnterAnim).begin();
    },

    handleMouseLeave: function(sender, eventArgs) {
        sender.findName(this.mouseLeaveAnim).begin();
    },

    handleMouseUp: function(sender, eventArgs) {
        sender.findName(this.mouseUpAnim).begin();
    	
	    if (this.clickHandler) {
	        this.clickHandler(sender, eventArgs);
	    }
    },

    handleMouseDown: function(sender, eventArgs) {
      sender.findName(this.mouseDownAnim).begin();
    }
}

I just put all of this code into it's own .js file, such as, "EventHandler.js." Then, you can make calls to this mouse event handling code like this:

// Bind XAML buttons to javascript code
        var GreenCircle = rootElement.findName("CnvGreenCircle");
        var gcEventHandler = new EventHandler();
        gcEventHandler.setTarget(GreenCircle); 
        gcEventHandler.setClickHandler(Silverlight.createDelegate(this,
 this.onCnvGreenCircleClicked));
        gcEventHandler.setMouseEnterAnim("SbGreenCircle_MouseEnter");
        gcEventHandler.setMouseLeaveAnim("SbGreenCircle_MouseLeave");
//        gcEventHandler.setMouseDownAnim("SbGreenCircle_MouseDown");
//        gcEventHandler.setMouseUpAnim("SbGreenCircle_MouseUp");

        var handleLoader = gcEventHandler.handleLoad();
        this.createDelegate( gcEventHandler,  handleLoader);

A few things to note about this code sample (which, for us, sets up the mouse event handlers from within the onLoad function of our main page/screen Javascript):

  1. I don't have to pass in all 4 mouse events. Check out the lines that are commented out.
  2. The Event Handler class is set-up to have animations (storyboards) passed in for each mouse event. Each of those "Sb..." parameters are names of storyboards in our XAML, for instance.
  3. Notice the "setClickHandler." You can pass in additional click handling support. This is totally borrowed from the quickstart/sample that I got from the Microsoft documentation. But, basically, I have a function in my main page/screen handling code that tells the application what to do when the object (i.e. canvas) is clicked. For instance, on the click event you could have a media element start or stop. 

August 28, 2007

Getting Started with Silverlight From the Ground Up

I've been wanting to write a basic and comprehensive "How-to," but one of the Silverlight masters has already done so. If you want a great intro to how-to get started with Silverlight, check out Laurence Moroney's: Zero to Hero: Getting Started with Silverlight.

August 19, 2007

baby steps

I've been buried the last 2 weeks in a very non-Silverlight project at work and so haven't had any time to dive deeper in learning and developing Silverlight, until late last week. We have an upcoming project that requires Silverlight. Yay!

As movement goes in the beginning, sometimes you move forward and sometimes you move backward. While I haven't been able to move forward on the audio player application, I have started working on implementing embedded Silverlight elements into an ASP.NET master page. And so, I'm exploring the many ways to use Silverlight in the broader context of web-based UI development.

Looking over the shoulder of our Flash developers, I see lots of Flash development that ends up with a UI that is completely determined by the .swf. Meaning, that the entire UI is contained within the context of the .swf, like www.converse.com.

I'm wondering if that is necessary or desirable in Silverlight. In trying to suss out that question, I'm starting on my hunch that while you can build your entire UI in Silverlight, there are some compelling reasons sometimes not to. And, I'm wondering then if integration with the other HTML/Javascript elements

August 06, 2007

why blog about silverlight

I thought I'd write to explain some of my goals for having a blog on interactive and web development.

I've been a bit reticent to blog. I have, however, rallied around the idea that I love it when I can get insight from others about problems they might be trying to solve or when I can ride along a person's learning curve and be able to watch them struggle toward a solution.

My goal, then, for writing this blog (mnmnmn) is to write about the process of learning Silverlight and interactive web development. I've been developing web sites for awhile now, for various companies, but am just now trying to learn how to do interactive web development. I work in a great environment, in one of the most creative and interesting places I've worked, and asked one day if I could start learning Flash. For most of my career as a web developer, I've been jumping back-and-forth between server side ASP, ASP.NET development (some SQL dev, too) and front-end development (and, lately, lots of XML/XSLT). But, now, I want to get into bringing my artistic interests and work interests together by doing interactive development.

Also, I'm very interested lately in sussing out Silverlight against the needs of community-based web sites and analytics. I've been mesmerized lately by the "Mad Science of Online Community" and am increasingly interested in making web apps that integrate analytics and measurement. It seems to me that Silverlight is well-suited out-of-the-box for each of these.

August 03, 2007

Silverlight RC 1

So, as many of you already know, the RC 1 candidate for Silverlight launched last week (at least I think it was last week; it might have been the Friday before...). While we were in our training (Silverlight A.I.R. -- Artists in Residence), some of us got tripped up on the differences in syntax between version 0.9 and 1.0.

I guess I'm re-stating the obvious, but watch out for those subtle differences in syntax. For instance:

[object].visibility = "Hidden" worked for version 0.9, but doesn't in RC 1.0. Rather,

[object].visibility = "Collapsed" works in RC 1.0.

The good news is that now that the Release Candidate is out there shouldn't be (allegedly) anymore unsupported syntax changes.

August 02, 2007

Silverlight AIR Training

I just spent the last week and a half out at Microsoft for an intensive, hands-on training in Silverlight (http://www.silverlight.net). I had a great time getting immersed in the new technology (it's just emerged to the public a couple months ago as Microsoft's exploration into the online interactive web application space; and, yes, I do sit next to a handful of fantastic Flash developers and designers; and, yes, we're still friends. I'm pushing for friendly co-existence and the kind of healthy co-operative clashing that can come from throwing 2 different tools at the same set of tasks.).

Ten of us sat in a conference room, divided into teams of 2, paired as a designer with a developer, provided with a mentor and a series of introductory lectures on the features of Silverlight and the Expression suite of tools (Expression Blend, Expression Design, Expression Media Encoder, etc.) , to build a demo Silverlight application.

One team built a greeting card maker, another built an IM-driven karaoke app, another built a page-turning RSS feed reader,  another built a robust e-learning useful video app that popped up secondary videos at certain markers in the main video, and my team-mate (Rob from VML; a great designer) and I built an audio player. Here's a screen-shot of our app:

Dblraudioscreen_2 

Some, or all, of these demo apps should be available online in the near future. We have plans to upload the audio player when we've ironed out some more of the kinks. I'll post locations of these demos to this blog in upcoming posts.

Keep coming back to mnmnmn to read more about Silverlight, interactive web development, web agency life and other related phenomena. I'm a web developer at Zaaz, Inc. (http://www.zaaz.com) and I'm moving to become an interactive developer focusing on Silverlight technology. So, I figured I would bring some people along for the ride: mnmnmn is that ride.

Categories

WynApse

Web Social Architecture

Poetry to Make You Sizzle - Steelroad

Blog powered by TypePad