Locations of visitors to this page


Silverlight 2.0 Interacting with html

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

Silverlight 2.0 Interacting with html

With SilverLight 2.0 you can interact and handle events with the html elements on your page.  Here is a simple example that places a select (drop down control) on a web page which will change the color of a ellipse on a SilverLight app.  So lets start with the html

 

<body style="height: 100%; margin: 0;">
    <form id="form1" runat="server" style="height: 100%;">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <br />
    <span>Select a Color </span>
    <select id="ddColor">
        <option>Red</option>
        <option>Blue</option>
        <option>Green</option>
    </select>
    <br />
    <br />
    <div style="height: 100%;">
        <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/HtmlAndSilverlight.xap"
            MinimumVersion="2.0.30923.0" Width="100%" Height="100%" />
    </div>
    </form>
</body>

 

 

Now the XAML

 

<UserControl x:Class="HtmlAndSilverlight.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Canvas x:Name="LayoutRoot" Background="Tan" >
        <Ellipse x:Name="el" Width="400" Height="300" Fill="Red"></Ellipse>
    </Canvas>
</UserControl>

 

 

Now build the app so we have intellisense for the controls.  Ok first off lets get access to the drop down (select) on the web page. Then we can use AttachEvent to handle the onchange event.   

 

Dim cbo As HtmlElement
Private Sub Page_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    cbo = HtmlPage.Document.GetElementById("ddColor")
    cbo.AttachEvent("onchange", AddressOf ColorChanged)
End Sub

 

Once the user selects a color we will change the color of the ellipse.  Here is the complete code listing

 

Imports System.Windows.Browser

Partial Public Class Page
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub
    Dim cbo As HtmlElement
    Private Sub Page_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        cbo = HtmlPage.Document.GetElementById("ddColor")
        cbo.AttachEvent("onchange", AddressOf ColorChanged)
    End Sub

    Private Sub ColorChanged(ByVal sender As Object, ByVal e As HtmlEventArgs)
        Dim x = CInt(cbo.GetAttribute("selectedIndex").ToString)
        Select Case x
            Case 0
                el.Fill = New SolidColorBrush(Colors.Red)
            Case 1
                el.Fill = New SolidColorBrush(Colors.Blue)
            Case 2
                el.Fill = New SolidColorBrush(Colors.Green)
        End Select
    End Sub
End Class

 

Hope this helps

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Ken Tucker on Thursday, October 09, 2008 1:49 PM
Permalink | Comments (7) | Post RSSRSS comment feed

Related posts

Comments

busby seo test id

Sunday, November 16, 2008 1:51 AM

busby seo test

uh nice

Busby SEO Test us

Tuesday, November 25, 2008 4:00 AM

Busby SEO Test

Well, I think there's much more simpler way of doing it.

Busby SEO Test us

Wednesday, November 26, 2008 4:13 PM

Busby SEO Test

thanks i really enjoy reading your post!

busby seo test us

Monday, December 01, 2008 6:32 AM

busby seo test

Me too very enjoying reading your post. i want to say hi to my friends who using same name with me.. lol!

Busby SEO Test us

Wednesday, December 03, 2008 8:27 AM

Busby SEO Test

Interesting. Thanks.

busby seo test us

Monday, December 22, 2008 9:48 PM

busby seo test

NIce combination. thanks for share.

No deposit bonus sa

Friday, January 02, 2009 8:37 PM

No deposit bonus

Me too very enjoying reading your post. i want to say hi to my friends who using same name with me.. lol!
thanks mate..
best regards from
david jones.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Monday, January 05, 2009 4:12 PM