现在的位置: 首页 > 综合 > 正文

(转)Better Silverlight 4.0 Installation and Upgrade Experience

2012年10月27日 ⁄ 综合 ⁄ 共 7107字 ⁄ 字号 评论关闭

Better Silverlight 4.0 Installation and Upgrade Experience

Perception is everything and how your application installs, loads and upgrades can make a big difference to how users feel about it. Out of the box, Silverlight doesn’t make things particularly easy, but don’t worry, with a little work, you can cover the bases.

There are a number of checks you need to make when a user first browses to a website that contains your Silverlight application. The figure below illustrates this.

SilverlightInstallDecisionTree

Checking Silverlight Installed

This is all done before your application is even loaded and is controlled through the scripts in Silverlight.js and some extra magic you’ll have to write.

One of the first things you need to do before your application can run in a browser is to ensure Silverlight is actually installed on the client and that the correct version is available. There’s a good paper here that explains all the details – it’s good but I thought it could be simplified slightly – hence this post.

In the standard html file that Visual Studio creates for you, add the code below to the onSilverlightError function – otherwise you won’t be able to detect whether an upgrade to the installed version of Silverlight is required (this seems like a bug – I haven’t tracked it down yet).

<script type="text/javascript">

    function onSilverlightError(sender, args) {

 

        var appSource = "";

        if (sender != null && sender != 0) {

            appSource = sender.getHost().Source;

        }

 

        var errorType = args.ErrorType;

        var iErrorCode = args.ErrorCode;

 

        // ADD THIS BLOCK!

        // For some reason the onUpgradeRequired event is never fired

        if (iErrorCode == 8001) {

            Silverlight.onUpgradeRequired();

            return;

        }

 

        ...

    }

...

</script>

Next add the following additional JavaScript code. These functions will allow you to control the HTML that is displayed depending on whether Silverlight is installed or the version that is available.

<script type="text/javascript" src="script/plugindetect.js"></script>

<script type="text/javascript" src="script/Silverlight.supportedUserAgent.js"></script>

<script type="text/javascript">

 

    Silverlight.onRestartRequired = function () {

        DisplayAltenativeContent(

            "<p>Almost there! You just need to restart your browser and to use this application.</p>");

    };

 

    Silverlight.onUpgradeRequired = function () {

        PluginDetect.getVersion('.');

        DisplayAltenativeContent(

            "<p>Your have version " + PluginDetect.getVersion('Silverlight') + " of Silverlight installed " +

            "but this application needs at least version " + getRequiredVersion() + ".</p>" +

            "<p>Click below to get the latest version.</p>" +

            Silverlight.buildPromptHTML(getRequiredVersion()));

    };

 

    Silverlight.onInstallRequired = function () {

 

        DisplayAltenativeContent(

            "<p>Silverlight doesn't appear to be installed (or may be disabled).</p>" +

            "<p>If you want to install Siverlight click below</p>" +

            Silverlight.buildPromptHTML(getRequiredVersion()));

    };

 

    DisplayAltenativeContent = function (html) {

        document.getElementById("plugin").innerHTML = "<div id='content'>" + html + "</div>";

    };

 

    getRequiredVersion = function () {

        return document.getElementById("minRuntimeVersion").getAttribute("value");

    }

 

    checkSupported = function () {

 

        // Check

        if (Silverlight.supportedUserAgent()) {

            document.getElementById("unsupported").innerHTML = "<p>Your browser does not officially support Silverlight, but it may still work (<a target='_blank' href='http://www.microsoft.com/silverlight/resources/install.aspx#sysreq'>more information</a>)</p>";

        }

    }

 

    // This function is called as the Silverlight plugin is being installed

    // and can be used to update controls in the pre-loader.

    onSourceDownloadProgressChanged = function (sender, eventArgs) {

        //var slPlugin = sender.getHost();

        //slPlugin.content.findName("progressMessage").Text = Math.round(eventArgs.progress * 100) + "%"; 

    };

 

</script>

Then call the checkSupport() function when the page loads. This warns the user if their browser is not officially supported. It’s important to not give up loading the plug-in since even browsers that aren’t on Microsoft’s official list may still be able to run it.

<body onload="javascript: checkSupport();">

And finally update the object block to include a few additional parameters (and for my example a few extra divs). In particular, note onUpgradeRequired, onInstalledRequired and onRestartRequired. These determine which functions will be called under the different scenarios and will be initiated by the code in Silverlight.js.

<div id="silverlight">

    <div id="plugin">

        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"

            width="100%" height="100%">

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

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

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

            <param id="minRuntimeVersion" name="minRuntimeVersion" value="9.0.50401.0" />

            <!-- add these parameters to get a splash screen to display -->

            <param name="splashscreensource" value="ClientBin/splash_spinner.xaml" />

            <param name="onSourceDownloadProgressChanged" value="onSourceDownloadProgressChanged" />

            <!-- add these parameters to display to Silvelight installation/upgrade instructions -->

            <param name="onUpgradeRequired" value="onUpgradeRequired" />

            <param name="onInstallRequired" value="onInstallRequired" />

            <param name="onRestartRequired" value="onRestartRequired" />

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

        </object>

        <iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;

            border: 0px"></iframe>

    </div>

    <div id="unsupported">

    </div>

</div>

To test the different behaviours, first change the minRuntimeVersion to something greater what you have installed, say, 9.0.50401.0. This will result in the following.

image

Note that in my implementation I’ve included PluginDetect to identify the version of Silverlight installed – it may be useful, in some scenarios, to let a user easily see what version they have installed and what is required. But obviously it’s up to you what information you decide to display – simply change the Silverlight.onUpgradeRequired definition. The link to install the required version is created from a call to Silverlight.buildPromptHTML(version) (this function is defined in Silverlight.js).

Next, try disabling the Silverlight plug-in on your favourite browser. Things now behave as they would if someone doesn’t have Silverlight installed at all. You should see something like this (based on what’s defined in Silverlight.onInstallRequired ),

 image

The next thing to check is using a browser that isn’t officially supported. Enable Silverlight again, reset the minRequiredVersion and on an unsupported browser you’ll get the application loading but with a warning underneath,

image

The other thing that’s easy to do is identify when a users accesses the in-browser application after they’ve already unstalled it on their machine. All you need to do is check whether App.Current.InstallState == InstallState.Installed and display something like this instead of the image above.

image

Updating Your (OOB) Application

If your Silverlight application, when displayed in the browser, is intended to launch an out of browser (OOB) application then you’ll want to make sure you include code to check for updates on the server. Remember, once a user installs the application on their computer, by default, it will never get updated, even when you put a new XAP up on the server.

In my example, I’ve created a page specifically tasked with checking for updates. This page is loaded every time the application starts and advises the user if a new update has been installed, otherwise it simply redirects to the main application page.

In this way I can create a nice animation to let the user know that the application is checking for updates.

image

The only code you really need to know from this page is,

App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);

 

App.Current.CheckAndDownloadUpdateAsync();

Simply, wire up a handler that will get called once CheckAndDownloadUpdateAsync() has completed. You can then tell whether an new version is available. If so, display something like,

image

抱歉!评论已关闭.