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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

As you can see, the xamlFragment is a string containing a new piece of XAML. This is<br />

created <strong>and</strong> a reference to it returned to the textBlock var. This reference is then added to<br />

the tree by adding it to the children of the root element.<br />

The findName Method<br />

XAML nodes may be named using their x:Name attribute. Should you want to manipulate<br />

the content of the XAML node (to, for example, change the contents of a TextBlock), you<br />

do so by using the findName method to get a reference to that node, <strong>and</strong> then editing the<br />

reference.<br />

This was shown in our first example, in the event h<strong>and</strong>ler for clicking the TextBlock.<br />

Here’s the code again:<br />

var <strong>Silverlight</strong>Control;<br />

var theTextBlock;<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 />

As you can see in the h<strong>and</strong>leLoad function, the findName method was called to get a<br />

reference to the TextBlock (called txt). This reference was then manipulated by adding an<br />

event h<strong>and</strong>ler to it.<br />

The createObject Method<br />

This method allows you to create a new object for a specific purpose. In version 1.0 of<br />

<strong>Silverlight</strong>, only one object is supported: the Downloader object. The Downloader object<br />

allows you to download additional elements using asynchronous downloading functionality.<br />

It is very similar in scope to Ajax. It supports a number of properties, methods, <strong>and</strong><br />

events that allow you to h<strong>and</strong>le the download.<br />

Using the Downloader Object<br />

The <strong>Silverlight</strong> control provides a Downloader object (created using the createObject<br />

method) that allows you to get additional application assets in an asynchronous manner.

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

Saved successfully!

Ooh no, something went wrong!