Page view counter

Reach Out And Touch HTML

I've posted a new video that shows how Silverlight 1.1 lets you create custom controls that can reach out to the HTML host page. In the example, we not only touch HTML controls, but we also respond to events (a button press) on an HTML control. The enabling technology is in the name space System.Windows.Browser.

The work turns on creating HtmlElement objects as member variables of your class.  These objects have an AttachEvent method that takes the name of the event and an event handler as arguments, as shown in this snippet:


private HtmlElement newMessageTextBox;
private HtmlElement updateButton;
private HtmlElement fontSizeTextBox;

public void Page_Loaded(object o, EventArgs e)
{
    InitializeComponent();

    HtmlDocument document = HtmlPage.Document;
    newMessageTextBox = document.GetElementByID("messageInput");
    updateButton = document.GetElementByID("update");
    updateButton.AttachEvent(
        "onclick", 
        new EventHandler(this.updateButton_Click)
    );
}

The HtmlPage is able to get the current (HTML) Document and from that we can get the (HTML) TextBox that was placed on the Document. With the document in hand, we can get the button, declared in TestPage.html as

<input id="update" type="button" value="Update" />

With a reference to the button, we can add the event handler that we can then implement in managed code. Very slick and reverses the visibility that was required in 1.0 (in which the HTML page was reaching into the Silverlight control).

 

Published 25 October 2007 12:53 PM by jesseliberty

Comments

# Reach Out And Touch HTML - Jesse Liberty - Silverlight Geek said on 25 October, 2007 02:02 PM

Pingback from  Reach Out And Touch HTML - Jesse Liberty - Silverlight Geek

# BenHayat said on 25 October, 2007 02:17 PM

This video is very educational. Great job on the video!

..Ben

# BenHayat said on 25 October, 2007 02:45 PM

Jesse, I have a question on your other video (XML vs C#). When you define objects declaratively in XAML (i.e. rectangular) and then when you compile/build the project, does the XAML file gets converted into C# and becomes part of the DLL? Or will the contents of XAML file get loaded into SL engine at runtime and then objects get created from reading raw XAML file?

I see that the XAML file has a "Build Action" that can bet set as "Embedded Resource", but what would that do for us?

My question is really in two folds: a) Isn't XAML file slower than if we created objects in C#?

b) How can we protect the contents of XAML file?

Thanks!

..Ben

# WynApse said on 25 October, 2007 06:31 PM

Silverlight Cream for October 25, 2007 -- #112

# Test said on 26 October, 2007 01:03 AM

I&#39;ve posted a new video that shows how Silverlight 1.1 lets you create custom controls that can reach

# From the software development trenches said on 26 October, 2007 01:29 AM

Time for another weekly round-up of developer news that focuses on .NET, agile and general development

# html » Reach Out And Touch HTML said on 27 October, 2007 04:20 PM

Pingback from  html &raquo; Reach Out And Touch HTML

# jesseliberty said on 30 October, 2007 11:54 AM

Ben,

If you create a Silverlight Control (using the class library option) and you choose Embedded resource, then you get a dll. As for "protecting" your XAML, if you mean protecting the Intellectual Property, I think you are on as difficult ground as always with .NET but I'll have to look into it further. Meanwhile I suggest something like this:

This message is copyright, trademark, contains trade secrets, trussreppers will be persecuted, company confidential, void where prohibited, no warranty express or implied, contents are hot, your mileage may vary, I never borrowed it, it was broken when you leant it to me and I returned it in perfect condition. Don't Panic. Answer the question or we'll gag you. What question? Gag him.

The opiinons expressed are not mine.

# SilverSpud » Blog Archive » My Favorite Blogs - Last 5 Posts as of 03 Nov 2007 said on 03 November, 2007 09:47 AM

Pingback from  SilverSpud  &raquo; Blog Archive   &raquo; My Favorite Blogs - Last 5 Posts as of 03 Nov 2007

# BigTundra said on 06 November, 2007 11:58 AM

Jesse,

I'm trying to run your code from this example and all of the document.GetElementByID(...) are returning null.  I am getting a message from IE that it has blocked content which I haven't seen before with silverlight... when I say to allow the content the page_loaded event fires but I still don't get any references to the HtmlElements defined.

Thanks for your help.

# video » Reach Out And Touch HTML said on 24 November, 2007 01:33 PM

Pingback from  video &raquo; Reach Out And Touch HTML

# http://silverlight.net/blogs/jesseliberty/archive/2007/10/25/reach-out-and-touch-html.aspx said on 04 April, 2008 09:23 AM

Pingback from  silverlight.net/.../reach-out-and-touch-html.aspx