RIA Development Center
Features Tips Events
Delivering business processing logic through RIA (Rich Internet Application) format is very attractive to architects, developers and operators of IT shops. RIA combines the richness of the desktop application with the ease of deployment, ubiquity and platform independence of the web application. RIAs in the form of mashups, are very popular in the consumer space but have yet to see similar success in the enterprise space. Enterprise applications are stateful and process-centric in nature. Read more
See more tips
Get regular email alerts when we publish new features!
DevX RIA Development Update

More Newsletters
Get Started with Silverlight Using Visual Studio 2008 and Expression Blend 2 (cont'd)
More Resources
  • MSDN: Microsoft Visual Studio 2008
        Downloads
  • Free Trial: Microsoft Expression Blend 2
  • Writing the Logic Using Visual Basic
    With the UI built, it's time to write the code to make it do something useful—for instance, display the current time in the button.

    Double-click the Page.xaml.vb file in Solution Explorer to load it in the code editor.

    Figure 14. Handlers: Creating the event handler for the Completed event.

    In the Page_Loaded() subroutine, add the following bolded lines:

    
    Partial Public Class Page
        Inherits Canvas
    
        Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)
            ' Required to initialize variables
            InitializeComponent()
    
            Me.Timeline1.Duration = New Duration(New TimeSpan(0, 0, 1))
            Me.Timeline1.Begin()
    
        End Sub
    
    In the preceding code, the Timeline1 triggers an event (the Completed event) each second (set through the Duration object). The Timeline object is somewhat similar to the Timer control that Windows developers are familiar with. The Begin() method starts the countdown and one second later the Completed event is fired.

    The next step would be to service the Completed event, which you can easily do by first selecting the Timeline1 object from the top of the code editor, and then selecting its corresponding Completed event (see Figure 14).

    Figure 15. Completed: Testing the application in both IE and FireFox.

    Code the Completed event handler as follows:

    
    Private Sub Timeline1_Completed( _
       ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Timeline1.Completed
    
            Dim textBlk As TextBlock = Me.btnTime.Children(1)
            textBlk.Text = Now.ToString
            Me.Timeline1.Begin()
    
        End Sub
    End Class
    
    Basically, you set the current time by retrieving the TextBlock control embedded within the canvas (btnTime) and setting its Text property. The canvas has two children:
    • Children(0): Rectangle control
    • Children(1): TextBlock control
    After displaying the time, you call the Begin() method to start the countdown again.

    That's it! Press F5 in Visual Studio 2008 and you will see your Silverlight application displayed in Internet Explorer. If you load the sample application in FireFox, it will look and work the same (see Figure 15). The button will update the time every second.

    Previous Page: Building the User Interface Using XAML Next Page: Transformation
    Page 1: IntroductionPage 5: Writing the Logic Using Visual Basic
    Page 2: Obtaining the ToolsPage 6: Transformation
    Page 3: Getting StartedPage 7: Deploying Silverlight Applications
    Page 4: Building the User Interface Using XAML 
    We have a winner in the RIA Run contest! Check out the Contest Winners Gallery and see which entries took the top prizes. You can play the games, too! Also, be sure to check out our interview with the grand prize winner to see how he crafted his winning entry. (Silverlight 2 Beta 2 required)