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 16 ■ PROGRAMMING SILVERLIGHT WITH XAML AND JAVASCRIPT 397<br />

You remove the event listener using the RemoveEventListener method on the control.<br />

You specify the event name <strong>and</strong> the h<strong>and</strong>ler token in the same way as you did when<br />

adding the event listener. Here’s an example:<br />

txtBlock.removeEventListener("mouseLeftButtonDown", h<strong>and</strong>leTxtClick);<br />

The CaptureMouse <strong>and</strong> ReleaseMouseCapture Methods<br />

The CaptureMouse method allows the object to receive mouse events even when the<br />

mouse isn’t within its borders. This is typically used for dragging events, so that once you<br />

start dragging an object, you don’t have to be within its bounds to continue dragging it.<br />

This is released using the ReleaseMouseCapture method.<br />

Here’s an example where they are being used in conjunction with a h<strong>and</strong>ler for the<br />

MouseMove event, which will be discussed later in this chapter:<br />

var isMouseCaptured;<br />

function h<strong>and</strong>leLoad(control, context, sender)<br />

{<br />

var txtBlock=control.content.findName("txt");<br />

txtBlock.addEventListener("MouseLeftButtonDown","mouseDown");<br />

txtBlock.addEventListener("MouseLeftButtonUp","mouseUp");<br />

txtBlock.addEventListener("MouseMove","mouseMove");<br />

isMouseCaptured = false;<br />

}<br />

function mouseDown(sender,mouseEventArgs)<br />

{<br />

sender.captureMouse();<br />

isMouseCaptured = true;<br />

}<br />

function mouseUp(sender, mouseEventArgs)<br />

{<br />

sender.releaseMouse();<br />

isMouseCaptured = false;<br />

}<br />

function mouseMove(sender, mouseEventArgs)<br />

{<br />

if(isMouseCaptured)<br />

{<br />

sender.setValue("Canvas.Top", mouseEventArgs.getPosition(null).x);<br />

sender.setValue("Canvas.Left", mouseEventArgs.getPosition(null).y);<br />

}<br />

}

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

Saved successfully!

Ooh no, something went wrong!