Today I started to play around with silverlight 1.0, now I don’t usually code in anything like Java Script, but coding in c# meant it wasn’t too bigger leap, and the small bit of hacking I have done for websites put me in good stead, so I thought it couldn’t be too difficult.
I created a small animation in blend and wanted a button that changed the background color when the user clicked on it, a simple play application that would allow me to see what was involved in the coding. All went well until I tried to hook up the click event, I added the attribute ‘MouseLeftButtonDown’ to the canvas element of my control setting its value to TestClickDown, and created a simple function within the java script which put up a dialog box, but I could just not get it to work, the function was never called, I googled the web, and looked at the silverlight help, but it talked about putting the two items that I had done, but nothing else, then I looked in the code supplied by blend as part of the project and saw this line
rootElement.addEventListener("MouseLeftButtonDown", Sys.Silverlight.createDelegate(this, this.handleMouseDown));
and the penny dropped, I hadn’t hooked up the event on the xaml to the function in the java script! Sounds simple but I had totally forgotten about it, and not being used to the script I was looking at hadn’t noticed the line, notice how this line hooks the root element, in Silverlight that is the controls canvas, (in my case just about the whole page!), not quite what I wanted, so I had to adjust the statement so it hooked only the canvas I wanted,
// set the event hookup for the specific canvas var canvas = this.control.content.findName("Click_TestBlock"); canvas.addEventListener("MouseLeftButtonDown", Sys.Silverlight.createDelegate(this, this.TestClickDown));
this searches the control for a canvers called Click_TestBlock, and then I set the event on that canvas, and as
they say in the story books all was well.
I am off to do some data binding next to text blocks and see how that works, calling a web service from script
is a new experience for me, and should be interesting.