Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display HTML in Silverlight application?

I would need to display some basic HTML (just some paragraphs, unordered lists and hyperlinks) in my Silverlight application. How would I go about that?

Which control to use?

like image 737
Richard Knop Avatar asked Oct 03 '10 09:10

Richard Knop


People also ask

What is Silverlight in HTML?

From simple islands of richness in HTML pages to full desktop-like applications in the browser and beyond, Silverlight enables applications that deliver the kinds of rich experiences users want.” These words underscore what is apparent to us: Silverlight and HTML5 will both thrive in the foreseeable future.

What is Silverlight an example of?

Silverlight is a Plug-in for a web browser. A Plug-in is an extension of an application created to enhance or extend functionality. Examples of other web browser plug-ins are Flash, Java applets, Windows Media Player, and QuickTime.


1 Answers

Try this link for starters: http://www.wintellect.com/CS/blogs/jprosise/archive/2009/12/22/silverlight-4-s-new-html-hosting-support.aspx

Here is the relevant part:

Another of the new capabilities that Silverlight 4 brings to the platform is the ability to host HTML content inside a Silverlight control. This support isn't limited to static HTML content; the content can be interactive and can include script. It can even be Flash content or content that includes other Silverlight controls.

To host HTML content in Silverlight, you can use either a WebBrowser control or an HtmlBrush. One way to display HTML content is to fire up a WebBrowser control and point it to a URL:

<WebBrowser x:Name="WebBrowserControl" Source="http://www.bing.com" />

Another way to do it is to call NavigateToString and pass a string of content to the WebBrowser control:

WebBrowserControl.NavigateToString("<h1>Hello, Silverlight</h1>");

HTML hosting is not available to in-browser apps (it applies to out-of-browser applications only), and if an OOB lacks elevated permissions, it can only display content that comes from the same domain as the Silverlight application. However, you can use a little trick to display cross-domain content in OOBs that run without elevated permissions—simply pass an IFRAME targeting the remote content to NavigateToString:

WebBrowserControl.NavigateToString("<iframe src=\"http://www.bing.com\" style=\"width: 100%; height: 100%\"></iframe>");

You can render HTML content with HtmlBrush, too. The following XAML snippet paints a Rectangle with content retrieved from Bing:

<WebBrowser x:Name="WebBrowserControl" Source="http://www.bing.com" />
<Rectangle>
  <Rectangle.Fill>
    <HtmlBrush SourceName="WebBrowserControl" />
  </Rectangle.Fill>
</Rectangle>

One difference between WebBrowser and HtmlBrush is that the former displays "live" content, while the latter does not. Another difference is that HtmlBrush can have transforms applied to it, while WebBrowser cannot. For snazzy visual effects involving HTML content like the HTML puzzle demoed at the PDC, you'll probably find yourself using HtmlBrush. To display live, interactive content, you'll find WebBrowser more useful instead.

One of the really cool things about the WebBrowser control is that you can use its InvokeScript method to call JavaScript functions in content hosted by the control. Conversely, JavaScript hosted inside a WebBrowser control can use window.external.Notify to raise ScriptNotify events that can be handled in C#.

like image 117
Gone Coding Avatar answered Nov 15 '22 09:11

Gone Coding