Silverlight嵌入HTML和Flash和PDF文件

在Silverlight中嵌入HTML、Flash和PDF文件,请按以下进行

  HtmlHost

  网站:http://www.divelements.co.uk/silverlight/tools.aspx

  下载:http://www.divelements.co.uk/silverlight/SilverlightTools.1.0.1.zip

控件下载:https://files.cnblogs.com/waren168/SilverlightTools.1.0.2.zip

  接着,就对该组件进行介绍并且通过实际范例讲述该组件的应用。

  1、基本特性

  组件所在命名空间:

  Divelements.SilverlightTools

  组件常用属性:

  SourceUri:显示在该组件中的内容的URI。

  SourceHtml:用该组件显示的超文本内容。

  Document:显示在该组件中的内容的HTML。

  组件常用事件:

  DocumentReady:当在该组件中的内容的HTML加载完毕后触发该事件。

  2、实例讲解

  注意:该组件需在windowless模式下才能正常运行。因此,首先要对服务端文件夹下的SilverlightClientTestPage.aspx文件或SilverlightClientTestPage.html文件做如下修改:(绿字粗体部分为新添加的内容)

<form  runat="server" >

    <div >

        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

          <param name="source" value="ClientBin/SilverlightClient.xap"/>

          <param name="onError" value="onSilverlightError" />

          <param name="background" value="white" />

          <param name="minRuntimeVersion" value="3.0.40624.0" />

          <param name="autoUpgrade" value="true" />

          <param name="windowless" value="true" />

          <a href="http://go.microsoft.com/fwlink/?Link >

              <img src="http://go.microsoft.com/fwlink/?Link alt="Get Microsoft Silverlight" />

          </a>

        </object><iframe  ></iframe></div>

    </form>

接着,将需要嵌入的Flash文件及PDF文件放置在服务端文件夹的根目录下。

右击SilverlightClient文件夹下的References,引入Divelements.SilverlightTools命名空间。按Ctrl+Shift+B编译。

然后,在SilverlightClient文件夹下新建一个Silverlight UserControl,将其命名为ControlHtmlHost.xaml。该文件内容如下:

<UserControl x:Class="SilverlightClient.ControlHtmlHost"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:divtools="clr-namespace:Divelements.SilverlightTools;assembly=Divelements.SilverlightTools"

    Width="900" Height="530">

    <Grid x:Name="LayoutRoot" Background="White">

        <divtools:HtmlHost Name="htmlHost"/>

    </Grid>

</UserControl>

以下是MainPage文件的内容:

<UserControl x:Class="SilverlightClient.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"       

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

  <Grid x:Name="LayoutRoot">

        <StackPanel>

            <!--导航栏-->

            <StackPanel Orientation="Horizontal" Background="WhiteSmoke">

                <Button x:Name="myHTML" Content="HTML内容" FontSize="14"/>

                <Button x:Name="myFlash" Content="Flash" FontSize="14"/>

                <Button x:Name="myPDF" Content="PDF文档" FontSize="14"/>

            </StackPanel>

            <Grid x:Name="Container"/>

        </StackPanel>

    </Grid>

</UserControl>

MainPage.xaml.cs文件代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Divelements.SilverlightTools;

namespace SilverlightClient

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

            //注册事件触发处理

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            this.myHTML.Click += new RoutedEventHandler(myHTML_Click);

            this.myFlash.Click += new RoutedEventHandler(myFlash_Click);

            this.myPDF.Click += new RoutedEventHandler(myPDF_Click);

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            GetRichContent("http://cn.bing.com", UriKind.Absolute);

        }

        void myPDF_Click(object sender, RoutedEventArgs e)

        {

            GetRichContent("/test.pdf",UriKind.Relative);

        }

        void myFlash_Click(object sender, RoutedEventArgs e)

        {

            GetRichContent("/clock.swf", UriKind.Relative);

        }

        void myHTML_Click(object sender, RoutedEventArgs e)

        {

            GetRichContent("http://cn.bing.com", UriKind.Absolute);

        }

        //获取Rich Content

        void GetRichContent(string uri,UriKind uk)

        {

            Container.Children.Clear();

            ControlHtmlHost chtml = new ControlHtmlHost();

            HtmlHost hh = chtml.FindName("htmlHost") as HtmlHost;

            hh.SourceUri = new Uri(uri, uk);

            Container.Children.Add(chtml);

        }

    }

}

以下官方说明

This is an ongoing effort to provide various simple controls and components of a production quality for free, for general use. Anybody can use these tools, whether in free or commercial software. At the moment we only have one control in the toolset, but we hope to expand it shortly. If you have any ideas (simple tools only please) feel free to contact us about them.

Note that there is no installer for these tools. Just extract the archive into a directory of your choice, read the license and use the choose items command in the Visual Studio toolbox to add the controls to your toolbox, if desired.

Download Silverlight Tools 1.0.2

HtmlHost

Many people have a need to display richly-formatted content in Silverlight, and at present the options for doing so are very limited. There is no document model or rich text facilities, and certainly no support for embedding HTML. This control allows you to embed HTML anywhere in a Silverlight application by simply specifying raw HTML to display or, if you prefer, the URL to a web page.

The control works by asking the host browser to create a DIV tag in which to display the HTML, and positioning it cleverly to match up with the Silverlight layout system. You should find that this "just works" as the HtmlHost control is dynamically moved or shown (on a tab page, for example) or even if the Silverlight plugin itself is moved. You must be using the Silverlight plugin in windowless mode to use this control.

This is how you use the control to host live or external web content.

<Grid x:Name="LayoutRoot" Background="White">

<Border Margin="30">

<!-- Embed HTML within the page -->

<divtools:HtmlHost Name="htmlHost" SourceUri="http://www.google.com" />

</Border>

</Grid>

And this is how you create HTML on the fly.

private void Button_Click(object sender, RoutedEventArgs e)

{

// Populate the control with html

StringBuilder html = new StringBuilder();

html.AppendLine("<h1>Heading</h1>");

html.AppendLine("<p>This is a paragraph <span >using</span> CSS <strong>formatting</strong>.</p>");

html.AppendLine("<ul><li>List Item 1</li><li>List Item 2</li></ul>");

htmlHost.SourceHtml = html.ToString();

}