Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

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).

 

Comments

Reach Out And Touch HTML - Jesse Liberty - Silverlight Geek said:

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

# October 25, 2007 2:02 PM

BenHayat said:

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

..Ben

# October 25, 2007 2:17 PM

BenHayat said:

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

# October 25, 2007 2:45 PM

WynApse said:

Silverlight Cream for October 25, 2007 -- #112

# October 25, 2007 6:31 PM

Test said:

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

# October 26, 2007 1:03 AM

From the software development trenches said:

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

# October 26, 2007 1:29 AM

html » Reach Out And Touch HTML said:

Pingback from  html &raquo; Reach Out And Touch HTML

# October 27, 2007 4:20 PM

jesseliberty said:

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.

# October 30, 2007 11:54 AM

SilverSpud » Blog Archive » My Favorite Blogs - Last 5 Posts as of 03 Nov 2007 said:

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

# November 3, 2007 9:47 AM

BigTundra said:

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.

# November 6, 2007 11:58 AM

video » Reach Out And Touch HTML said:

Pingback from  video &raquo; Reach Out And Touch HTML

# November 24, 2007 1:33 PM