====================== I'M A VB "Introduce" "Love users: lounge, "I'm a VB", army surplus shirts" ====================== CO-EVOLUTION. "Luca already explained; sow how putting into practice" "keynotes, Silverlight 4" "contrived example, but touch on all new features" "and of course applies also to desktop applications" ======================== NEW SILVERLIGHT APPLICATION "Goal is a web page that gathers together your twitter feed" * File>New, Drop on a ListBox * DesignWidth = 800 * FontSize="16" ItemsSource="{Binding}" * Drop on a Button, dbl-click on the button * Option Strict On ==================== POPULATE IT "Populate it with some data" "ObservableCollection" |snippet 1: new observablecollection |Dim c As New Collections.ObjectModel.ObservableCollection(Of Object) |c.Add("hello") |c.Add("there") |c.Add("world") * ListBox1.DataContext = c * Run ========================== DECLARATIVE "We're computer programmers; we don't like to do things more than once" * For Each s In New String() {"hello", _ "there", _ "world"} c.Add(s) Next s * Delete the underscores. "shout at me if you see more underscores!" * Delete the explicit type: "array literals" * dim matrix = {{1,0},{0,1}} dim inference = {1,2,3} "> WHY: small feature, sure, but use every day" "Here's another way to initialize the list:" * From { _ "hello", "there", "world" } * Run. ================================ CLASSES AND PROPERTIES "Get serious. Application is going to get a twitter feed, so a class for data." |snippet 2. class Tweet - verbose |Class Tweet | Private _Name As String | Public Property Name As String | Get | Return _Name | End Get | Set(ByVal value As String) | _Name = value | End Set | End Property |End Class * Delete it: "auto-properties" |Snippet 3. tweet - filler |Public Property Time As DateTime |Public Property Title As String |Public Property Link As Uri | |Public Overrides Function ToString() As String | Return Time & " - " & Name & " - " & Title |End Function "Dustin's talk has real fast IDE tricks for this" "> WHY: another small feature used every day" * Move c into a class field * Move ListBox1.DataContext = c into the InitializeComponents * Double-click on button, Delete event args |snippet 4. c.Add(new Tweet) |c.Add(New Tweet With {.Name = "Sue", .Title = "I saw a neat new #vb10 feature at #pdc09"}) |c.Add(New Tweet With {.Name = "Ben", .Title = "@Ben what was it?"}) |c.Add(New Tweet With {.Name = "Sue", .Title = "#vb10 has Auto-Properties"}) * Run. =================================================== DOWNLOAD THE DATA * Delete the c.Add, then |snippet 5. Dim client.DownloadStringAsync |Dim client As New WebClient |client.DownloadStringAsync(New Uri("http://search.twitter.com/search.atom?q=%23pdc09")) * AddHandler client.DownloadStringCompleted, AddressOf onCompleted and generate it from the drop-down "That's no good. Pollutes the class with useless members. And lose variables" * MessageBox.Show(client.BaseAddress) * Sub cut and paste. "> WHY: multiline lambdas will be useful for Silverlight, TPL, everything" "Now time for some XML" * AddReference "Do it again. See how fast it was!" * Dim rss = Xml.Linq.XElement.Parse(e.Result) "Oh, distributed system is one where your program stops working because of a problem in a machine across the world that you've never even heard of" * AddFile of "OfflineDefaults" * Dim rss = If(e.Error is Nothing, Xml.Linq.XElement.Parse(e.Result), defaultRss) * Also change source to http://herbert.wischik.com/tweet.exe?q=%23pdc09 "What does this XML actually look like?" * Look at it in IE, view source, copy+paste into File>New>XML-schema * Imports * Dim entries = From entry in rss. _ Select tweet = New Tweet With { _ .Name = entry...Value, _ |snippet 5. rest of LINQ |.Title = entry..Value, _ |.Time = DateTime.Parse(entry.<updated>.Value), _ |.Link = New Uri(entry.<link>.@href) _ |} |Order By tweet.Title Like "*#vb10*" Descending "> WHY: these XML literals come from VS2008. But I think now with XAML and Silverlight they're coming into their own. You as VB developers will be uniquely suited to ride the coming wave." "XML is like violence" * c.ReplaceWith(entries) * ' Tweet -> Object * ' IEnumerable(Of Tweet) -> IEnumerable(Of Object) * c.ReplaceWith(entries.Cast(Of Object)) "What's up? You'd think it should work. Mixed message here. It does on desktop apps through a feature called "co and contra-variance". The important thing is that it just works how you want, like in this example. Thorny details. Basilisk." * c.ReplaceWith({}) at top. * Run "> WHY: We've been coding WPF, we've covered most of VB's new language features, and everything here is desktop as well. VB's XML literals. "Now change gear and talk about Silverlight. Interact with the DOM -- Document Object Model -- the structure of the web page itself. Interop from the web page into VB, and from VB into the web page. Under the hood it uses DLR, Dynamic Language Runtime, part of co-evolution." |snippet 7. SL_MAINPAGE |Windows.Browser.HtmlPage.RegisterScriptableObject("SL_MAINPAGE", Me) |snippet 8. Button2_Click |<Windows.Browser.ScriptableMember()> |Public Sub Button2_Click() | MessageBox.Show("I am in VB") |End Sub * Then in ASPX page, add id="SLP" |snippet 9. <input id="Button2"> |<input id="Button2" type="button" value="VB" | onclick="document.getElementById('SLP').Content.SL_MAINPAGE.Button2_Click();" /> * <div id="vbcontent">This will be replaced by XML literals</div> |snippet 10. Dim document As Object |Dim document As Object = Windows.Browser.HtmlPage.Document * document.getElementById("vbcontent").innerHTML = <p> <i>I am VB</i> <hr/> <b>HEAR ME ROAR</b> </p> * delete Option Strict On * Run it. "> WHY: XML literals straight into DOM. Very few other platforms which have such an easy way to write XML." |snippet 11. image body |Static img As String = "" |Dim client As New WebClient |client.DownloadStringAsync(New Uri("http://localhost/ConsoleApplicationVB.exe")) |AddHandler client.DownloadStringCompleted, | Sub(sender As Object, e As DownloadStringCompletedEventArgs) | Dim x = If(e.Error Is Nothing, | Xml.Linq.XElement.Parse(e.Result), | defaultImages) | Dim imgs = From i In x.Nodes | Skip While i.ToString <> img | Skip 1 | img = imgs.Concat({x.Nodes.First}).First.ToString | document.getElementById("vbcontent").innerHTML = img | End Sub * Run it "Web-service." "Related work."