Silverlight_how to: Add Silverlight to a web page by using html

Source: Internet
Author: User
How to: Add Silverlight to a web page by using html Silverlight

The object element enables you to embed and configure the Silverlight plug-in your HTML in a way that is compatible with all supported browsers. this topic describes how to accomplish the following common tasks using the object element:

  • Embed the Silverlight plug-in and specify the application to host.

  • Specify alternate HTML to display when Silverlight is not installed.

These tasks correspond to different parts of an HTML page and specifically to different configuration parameters of the object element. the following procedures describe several tasks in isolation, but build up to a complete cross-browser HTML example at the end of this topic.

You shoshould use the final example as a template for your projects instead of using the snippets in each procedure. the final example ensures cross-browser compatibility and is based on the template that Visual Studio and expression blend use to dynamically generate test pages. note, however, that this example provides only a basic installation and upgrade experience. for more information on improving this experience, see Microsoft Silverlight installation experience White Paper and sample code.

You can accomplish Additional configuration tasks using the html object element. for more information, see Silverlight plug-in object reference and property values of the html object element. the topics listed in the See also Section provide additional coverage of specific embedding scenarios.

An alternative to using the object element is to use the Javascript embedding functions provided by the Silverlight. JS helper file. these functions ultimately generate object elements, and are provided as a convenience in Javascript development. for more information, see How to: Add Silverlight to a web page by using JavaScript.

Procedures

To embed the plug-in
  • Add the object element to your HTML and specify attributes and a child Param element as shown in the following example.

    Note:

    You will typically specify additional HTML to provide an installation experience and ensure cross-browser compatibility. For a complete HTML example, see the end of this topic.

    Copy
    <object width="300" height="300"    data="data:application/x-silverlight-2,"     type="application/x-silverlight-2" >    <param name="source" value="SilverlightApplication1.xap"/></object>

    The width and height attributes are required for cross-browser compatibility. you can specify fixed pixel values or percentages relative to the width and height of the parent element. if you use relative sizing, you can respond to plug-in size changes by handling the content. resized event. for more information, see Silverlight plug-in sizing.

    The Type attribute and the specific value shown are also required. this value uses the Silverlight MIME type to identify the plug-in and the required version. note that the indicated MIME type is used with Silverlight version 2 and all later versions. for more information, see Silverlight plug-in versioning.

    The data attribute and its value are recommended to avoid performance issues on Some browsers. Note the trailing comma in the data value. This indicates a second data parameter that has an empty value.

    The param element named source is required, and indicates the location and name of your application file. you typically specify. xap application package in a location relative to the HTML file. for more information, see source (Silverlight plug-in object ). for more information about application development, see application model.

To specify alternate HTML to display when Silverlight Is Not Installed
  • Add HTML content to the object element after the child Param elements, as shown in the following example.

    Copy
    <object id="SilverlightPlugin1" width="300" height="300"    data="data:application/x-silverlight-2,"     type="application/x-silverlight-2" >    <param name="source" value="SilverlightApplication1.xap"/>    <!-- Display installation image. -->    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0"         style="text-decoration: none;">            </a></object>

    This example shows the default install image source and Installer Uris. with these Uris, the server detects the user's browser settings to provide the correct version of the installation image and installer. if the user's browser is not supported, clicking the image causes the browser to open the Silverlight requirements page.

    You can provide arbitrarily complex alternate HTML in order to integrate the Silverlight install experience with your web page. however, in container cases, users will have to restart or refresh their browsers after installing Silverlight. with Internet Explorer, only a browser refresh is required unless the user has an older version of Silverlight installed and upgrades through the installation link.

    You can refresh the browser automatically or eliminate the refresh requirement by using helper functions in the Silverlight. JS file. you can also use Silverlight. JS to perform fine-grained browser requirements detection. for more information, see the Microsoft Silverlight installation experience White Paper and sample code and the Silverlight. JS reference.

Example

Description

The following code example provides a complete HTML page for a Silverlight application that uses the entire browser window. this example is based on the default HTML used by Visual Studio when you choose to dynamically generate a test page.

This example uses Cascading Style Sheets (CSS) and a div element to contain the plug-in. this ensures that the plug-in extends to the edges of the browser window. this and other additions to the HTML Help ensure cross-browser compatibility.

Note:

Because of browser differences, the Silverlight plug-in does not support the Cascading Style Sheets (CSS) overflow property on the object element or on a parent container element, such as a div element.

The IFRAME element is also for cross-browser compatibility. the presence of the IFRAME prevents the Safari browser from caching the page. safari caching prevents the Silverlight plug-in from reloading when the user navigates back to a previusly-visited Silverlight page. for more information, see the safari developer FAQ.

This example uses a JavaScript function to handle the plug-in's onerror event. A JavaScript error handler is useful during debugging, But you typically remove it when you deploy your application.

This example also includes minruntimeversion and autoupgrade settings to provide an upgrade experience if the specified version of Silverlight is not installed. (As mentioned previusly, the "2" in the type and data settings do not indicate the Silverlight version .) for more information, see Silverlight plug-in versioning.

To view this example in your web browser window, you must specify a valid Silverlight application package in the source parameter. For more information, see How to: create a new Silverlight project.

Code

Copy
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">See also

Taskshow: specify and retrieve custom initialization parameters referencesilverlight plug-in object reference conceptsadding Silverlight to a web page by using HTML or Javascript Silverlight plug-in sizing Silverlight splash screens security settings in HTML bridge oresourceshtml Bridge: interaction between HTML and managed code Silverlight requirements Microsoft Silverlight installation experience White Paper and sample code Community contentAdd FAQ

If the above example does not work, you may need to add clientbin I struggled a lot to get Silverlight 3.0 to work for me prior to finding this. most of the examples I found on the Web were based on 1.0 or 2.0. the only thing I had to change in the above example was to use:
<Param name = "Source" value = "clientbin/silverlightapplication1.xap"/>
Instead:
<Param name = "Source" value = "silverlightapplication1.xap"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.