<a href="http://webabcd.blog.51cto.com/1787395/342790" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/100302">[源碼下載下傳]</a>
穩紮穩打Silverlight(30) - 2.0Tip/Trick之Silverlight.js, Silverlight.supportedUserAgent.js, 自定義啟動界面, 響應滑鼠滾輪事件
介紹
Silverlight 2.0 提示和技巧系列
Silverlight.js - 一些 js 幫助函數,用于嵌為入 Silverlight 插件以及自定義安裝體驗等提供幫助
Silverlight.supportedUserAgent.js - 就一個函數,用于判斷 Silverlight 是否支援使用者的浏覽器
自定義啟動界面 - 三個參數的綜合應用:splashScreenSource, onSourceDownloadProgressChanged, onSourceDownloadComplete
響應滑鼠滾輪事件 - 響應并處理滑鼠的滾輪事件
線上DEMO
示例
1、Silverlight.js 和 Silverlight.supportedUserAgent.js 的常用函數的示範和說明
SilverlightDotJsDemo.html
<!--
詳解 Silverlight.js
Silverlight.js - 一些 js 幫助函數,用于嵌為入 Silverlight 插件以及自定義安裝體驗等提供幫助,其最新版本在如下位址下載下傳
http://code.msdn.microsoft.com/silverlightjs
Silverlight.supportedUserAgent.js - 就一個函數,用于判斷 Silverlight 是否支援使用者的浏覽器,其最新版本在如下位址下載下傳
http://code.msdn.microsoft.com/SLsupportedUA
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Silverlight.js</title>
<script type="text/javascript" src="../Silverlight.js"></script>
<script src="../Silverlight.supportedUserAgent.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</div>
<script type="text/javascript">
// Silverlight.createObject() - 生成一個嵌入了 Silverlight 插件的 object 元素
Silverlight.createObject(
"../ClientBin/Silverlight20.xap", // .xap 的位址
document.getElementById('container'), // 包含此 object 元素的父元素
"slPlugin", // object 元素的 id
{
width: "100%",
height: "100%",
minRuntimeVersion: "2.0.31005.0"
}, // Silverlight 插件的屬性數組
onLoad: onSLLoad,
onError: onSLError,
onSourceDownloadComplete: onSourceDownloadComplete
}, // Silverlight 插件的事件處理程式數組
"key1=value1,key2=value2", // 為 Silverlight 程式傳遞初始化參數(key=value的形式)。用“,”分隔
"myContext" // 上下文資訊,可以在插件的 onLoad 事件中擷取
);
function onSLLoad(plugin, userContext, sender) {
alert(plugin.id + " - " + userContext + " - " + sender.toString());
}
function onSLError(sender, args) {
// args - Sys.UI.Silverlight.ErrorEventArgs 類型
// ErrorEventArgs.errorType - 錯誤類型
// ErrorEventArgs.errorMessage - 錯誤資訊
// ErrorEventArgs.errorCode - 錯誤代碼
// 程式 throw 出的異常可以在此處捕獲到
alert(args.errorType + "\n" + args.errorMessage + "\n" + args.errorCode);
function onSourceDownloadComplete(sender, args) {
alert("SourceDownloadComplete");
// Silverlight.createObjectEx(params) - 将所有參數放入一個數組中傳入,其内部會解析這個數組中的參數,然後調用 Silverlight.createObject()
// Silverlight.default_error_handler = function (sender, args){} - onError 事件的預設處理程式,不需要的話可以将其置為 null
</script>
window.onload = function() {
// getSilverlight() - 嘗試下載下傳指定的版本,如果指定空字元串 "" 則為嘗試下載下傳最新版本
// Silverlight.getSilverlight("2.0.31005.0");
// isInstalled() - 判斷是否安裝了指定的 Silverlight 版本
alert(Silverlight.isInstalled("2.0.31005.0"));
// Silverlight.onSilverlightInstalled - 使用 Silverlight.js 時,如果用戶端沒有安裝 Silverlight 插件,則會自動安裝,然後調用此方法以重新整理浏覽器,可以重寫此方法以自定義行為(比如在此通過 createObject() 來使新安裝的插件生效,而無需重新整理)。注意:如果是 Silverlight 更新,則不會調用此方法,必須重新開機浏覽器(隻重新整理是不行的)
// supportedUserAgent(version, userAgent) - 判斷 Silverlight 是否支援使用者的浏覽器,省略 userAgent 則為目前浏覽器
alert(Silverlight.supportedUserAgent("2.0"));
</body>
</html>
2、自定義 Silverlight 程式的啟動界面,并顯示加載進度
啟動界面的 xaml 檔案
SplashScreen.xaml
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Bisque"
>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Loading" Margin="3" />
<TextBlock x:Name="percent" Text="0%" Margin="3" />
</StackPanel>
</Grid>
Silverlight 程式全部加載完成前,顯示啟動界面并顯示加載進度
SplashScreen.html
<title>SplashScreen</title>
<style type="text/css">
html, body
{
height: 100%;
overflow: auto;
body
padding: 0;
margin: 0;
#silverlightControlHost
</style>
<div id="silverlightControlHost">
<object id="xaml1" data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="../ClientBin/Silverlight20.xap" />
<!--下載下傳 source 指定的 xap 的過程中所顯示的 xaml 的位址-->
<param name="splashScreenSource" value="SplashScreen.xaml" />
<!--下載下傳 source 指定的 xap 的過程中所持續調用的事件-->
<param name="onSourceDownloadProgressChanged" value="onSourceDownloadProgressChanged"/>
<!--souce 指定的 xap 被完全下載下傳後所觸發的事件-->
<param name="onSourceDownloadComplete" value="onSourceDownloadComplete" />
</object>
<iframe style='visibility: hidden; height: 0; width: 0; border: 0px'></iframe>
function onSourceDownloadProgressChanged(sender, args) {
// progress 屬性 - 下載下傳進度(0 - 1 之間)
sender.findName("percent").Text = Math.round(args.progress * 10000) / 100 + "%";
sender.findName("percent").Text = "100%";
3、響應并處理滑鼠的滾輪事件
Wheel.xaml
<UserControl x:Class="Silverlight20.Tip.Wheel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox x:Name="lblMsg" />
</UserControl>
Wheel.xaml.cs
/*
* 如何響應滑鼠滾輪事件,可以參看 Deep Zoom Composer 生成的 MouseWheelHelper.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 System.Windows.Browser;
namespace Silverlight20.Tip
{
public partial class Wheel : UserControl
{
public Wheel()
InitializeComponent();
this.Loaded += new RoutedEventHandler(Wheel_Loaded);
void Wheel_Loaded(object sender, RoutedEventArgs e)
HtmlPage.Window.AttachEvent("DOMMouseScroll", OnMouseWheel);
HtmlPage.Window.AttachEvent("onmousewheel", OnMouseWheel);
HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel);
private void OnMouseWheel(object sender, HtmlEventArgs args)
args.PreventDefault();
double mouseDelta = 0;
ScriptObject eventObj = args.EventObject;
// Mozilla and Safari
if (eventObj.GetProperty("detail") != null)
mouseDelta = ((double)eventObj.GetProperty("detail"));
}
// IE and Opera
else if (eventObj.GetProperty("wheelDelta") != null)
mouseDelta = ((double)eventObj.GetProperty("wheelDelta"));
// IE浏覽器:mouseDelta == 120 向上滾動;mouseDelta == -120 向下滾動
// FF浏覽器:mouseDelta == -3 向上滾動;mouseDelta == 3 向下滾動
lblMsg.Text += mouseDelta.ToString();
}
}
OK
<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343939,如需轉載請自行聯系原作者