08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

360<br />

CHAPTER 15 ■ ENHANCING THE WEB EXPERIENCE WITH SILVERLIGHT<br />

function h<strong>and</strong>leLoad(control, userContext, rootElement)<br />

{<br />

<strong>Silverlight</strong>Control = control;<br />

theTextBlock = <strong>Silverlight</strong>Control.content.findName("txt");<br />

theTextBlock.addEventListener("MouseLeftButtonDown", "txtClicked");<br />

}<br />

function txtClicked(sender, args)<br />

{<br />

theTextBlock.Text = "Hello to you too!";<br />

}<br />

Now what will happen is that when the <strong>Silverlight</strong> control is rendered, the code finds<br />

the TextBlock element <strong>and</strong> creates a new event listener that responds to a mouse click by<br />

calling the JavaScript function txtClicked.<br />

This function will then change the contents of the TextBlock to “Hello to you too!” the<br />

end result being that the text will be changed as soon as the user clicks on it.<br />

Putting It All Together in HTML<br />

As you’ve seen already, HTML is at the heart of the <strong>Silverlight</strong> experience. <strong>Silverlight</strong> is a<br />

plug-in for the browser, after all. You’ve seen a number of pieces of this HTML already,<br />

but for reference, the complete HTML is here.<br />

This contains the JavaScript references to the <strong>Silverlight</strong>.js, Create<strong>Silverlight</strong>.js,<br />

<strong>and</strong> Code.js files, which are the <strong>Silverlight</strong> SDK script, the script containing the<br />

create<strong>Silverlight</strong> function, <strong>and</strong> the script containing the event-h<strong>and</strong>ler code, respectively.<br />

It also sets up a tag to define the dimensions of the that contains the<br />

<strong>Silverlight</strong> control, <strong>and</strong> the itself, which is populated by the results of the call to<br />

create<strong>Silverlight</strong>.<br />

<br />

<br />

First<strong>Silverlight</strong><br />

<br />

<br />

<br />

<br />

.silverlightHost {<br />

height: 480px;<br />

width: 640px;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!