15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

1110 ❘ ChaPTer 38 silverliGht<br />

private void OnRegistration(object sender, RoutedEventArgs e)<br />

{<br />

CurrentAttendee.EventId = (comboEvents.SelectedItem as Event).Id;<br />

var client = new WebClient();<br />

client.DownloadStringCompleted += (sender1, e1) =><br />

{<br />

if (e1.Error != null)<br />

{<br />

NavigationService.Navigate(<br />

new Uri("/ErrorPage", UriKind.Relative));<br />

}<br />

bool result = bool.Parse(XElement.Parse(e1.Result).Value);<br />

if (result)<br />

NavigationService.Navigate(<br />

new Uri("/Success", UriKind.Relative));<br />

else<br />

NavigationService.Navigate(<br />

new Uri("/ErrorPage", UriKind.Relative));<br />

}<br />

}<br />

}<br />

};<br />

Uri requestUri = new Uri(String.Format(<br />

"../../EventRegistrationDataService.svc/AddAttendeename='{0}'" +<br />

"&email='{1}'&company='{2}'&registrationCode='{3}'&eventid={4}",<br />

CurrentAttendee.Name, CurrentAttendee.Email,<br />

CurrentAttendee.Company, CurrentAttendee.RegistrationCode,<br />

CurrentAttendee.EventId), UriKind.Relative);<br />

client.DownloadStringAsync(requestUri);<br />

code snippet EventRegistration/Home.xaml.cs<br />

When you run the application, you can register for an event.<br />

Since version 3 of Silverlight, the Silverlight application does not have to use the networking stack from<br />

the browser. The networking stack from the browser is limited in the methods of the HTTP requests it can<br />

perform <strong>and</strong> returns only limited information on the response status codes. However, now it’s possible to<br />

use a different networking stack that can be selected with the WebRequestCreator class from the System<br />

.Net.Browser namespace. The following code snippet changes the networking stack to ClientHttp.<br />

BrowserHttp is the second option in WebRequestCreator. This change switches the networking stack to<br />

use all the networking options you’ve seen.<br />

bool result = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);<br />

broWser inTegraTion<br />

Integration with a web browser often is an important part of using Silverlight. Silverlight 1.0 could only be<br />

programmed using JavaScript. Luckily, this changed with Silverlight 2.0, but there are still many scenarios<br />

where you might want to control HTML <strong>and</strong> JavaScript code from within the Silverlight control or invoke<br />

.<strong>NET</strong> methods from JavaScript. Both are possible with the help of the namespace System.Windows<br />

.Browser.<br />

To demonstrate this integration by using the existing web solution, the following example creates a new<br />

Silverlight project named JavaScriptInterop. This control is used to demonstrate both calling out to the<br />

HTML code from within the Silverlight control <strong>and</strong> to invoke .<strong>NET</strong> methods from JavaScript.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!