Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links

News

Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories

Archives

Post Categories

.NET Framework

Blogroll

Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Using statement lambdas in Visual Basic with Windows Phone 7

I have written several apps for Windows Phone 7 with Visual Basic 2010, and I made an intensive usage of statement lambdas.

Some of my apps require downloading RSS feeds from the Internet and this is accomplished by instantiating the WebClient class. For instance, consider the following code that retrieves some string content from the Internet and loads the content into an XDocument:

         Dim wclient As New WebClient()
 
         'The statement lambda is a replacement for AddressOf
        AddHandler wclient.OpenReadCompleted, Sub(sender As Object, e As OpenReadCompletedEventArgs)
                                                  If e.Error IsNot Nothing Then
                                                      MessageBox.Show(e.Error.Message)
                                                      RaiseEvent ErrorThrown()
                                                      Exit Sub
                                                  End If

                                                  Dim str As IO.Stream = e.Result
                                                  Dim xdoc As XDocument = XDocument.Load(str)

                                                  ' Process your xdoc here...
                                                  ' close
                                                  str.Close()
                                                  ' add results to listbox
                                                  panoramaElement.DataContext = rssFeedsItems
                                              End Sub
        wclient.OpenReadAsync(rssUri)

Instead of writing a dedicated event handler, I wrote a statement lambda directly in the AddHandler clause in place of the AddressOf pointing to a delegate. Also notice how the lambda receives arguments of the type that you would expect in a classic event handler. This makes easier interacting with properties from the objects involved in the data processing.

I love this kind of approach since to me this makes easier having all the code for one scenario in one place. I do not need to navigate through the code to find the explicit event handler.

Just my opinion, but as you can see statement lambdas can be leveraged in the Windows Phone development exactly as you would do in other technologies.

Alessandro

Print | posted on giovedì 23 giugno 2011 17:31 | Filed Under [ Visual Basic ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 5 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET