修改jQuery的Plug-in插件以相容於Silverlight 2.0。
有在網頁中使用 Silverlight 應用程式的人通常都會使用 JavaScript 和它溝通(使用 HTML Bridge),而在 jQuery 流行起來以後,網路上也有出現 jQuery 的 Silverlight 插件 (plug-in),我找到的是這個版本:http://malsup.com/jquery/ag/,原本開心的要使用它,結果卻出現:
You must include the Silverlight.js script.
可是在頁面中已經引用了 Silverlight.js 了 ... 沒道理不能用啊。
這個 jquery.silverlight.js 還算簡單,長度不長,所以我大概看了一下它的內容,和 Silverlight 2.0 附的 Silverlight.js 比對了一下,發現在插件中使用了:
1
if (!window.Sys || !window.Sys.Silverlight) {
2
alert('You must include the Silverlight.js script.');
3
return;
4
}
if (!window.Sys || !window.Sys.Silverlight) { 2
alert('You must include the Silverlight.js script.'); 3
return; 4
}和 Silverlight.js 比對,Silverlight 2.0 中沒有 window.Sys 的宣告,所以它才不能使用。
因此,只要改一些 jquery.silverlight.js 中的兩個地方,就可以用在 Silverlight 2.0 啦(註解中標記 CHANGE FOR SILVERLIGHT 2.0 的部份,原本都有 "Sys.Silverlight",把 Sys 拿掉即可):
01
/**
02
* jQuery Plugin for use with Microsoft Silverlight
03
* Tested with Silverlight 1.0 beta
04
* @author: M. Alsup (malsup at gmail dot com)
05
* @version: 1.0 (5/04/2007)
06
* Documentation and examples at: http://www.malsup.com/jquery/ag/
07
* Free beer and free speech. Enjoy!
08
*/
09
(function($) {
10
11
// CHANGE FOR SILVERLIGHT 2.0
12
if (!window || !window.Silverlight) {
13
alert('You must include the Silverlight.js script.');
14
return;
15
}
16
17
$.fn.silverlight = function(opts) {
18
return this.each(function() {
19
var o = jQuery.extend({}, $.fn.silverlight.defaults, opts);
20
21
// try to use metadata plugin
22
if ($.meta) jQuery.extend(o, $(this).data());
23
24
// make sure activex object gets a unique id
25
var id = o.id || (this.id ? this.id + 'AG' : 'AG' + $.fn.silverlight.counter++);
26
var props = {
27
width: o.width,
28
height: o.height,
29
inplaceInstallPrompt: o.installPrompt,
30
background: o.bg,
31
isWindowless: o.windowless,
32
framerate: o.framerate,
33
version: o.version
34
};
35
var events = {
36
onError: o.error,
37
onLoad: o.load
38
};
39
40
// CHANGE FOR SILVERLIGHT 2.0
41
Silverlight.createObjectEx({
42
source: o.xaml,
43
parentElement: this,
44
id: id,
45
properties: props,
46
events: events,
47
initParams: o.params,
48
userContext: o.context
49
});
50
});
51
};
52
53
// @see http://msdn2.microsoft.com/en-us/library/bb190632.aspx
54
$.fn.silverlight.defaults = {
55
width: '300', // width of component in px
56
height: '300', // height of component in px
57
bg: '#00000000', // background color (default is transparent)
58
installPrompt: 'true', // display in-place install prompt?
59
windowless: 'true', // windowless mode (false for wrapping markup)
60
framerate: '24', // maximum framerate
61
version: '0.9', // Silverlight version
62
error: null, // onError callback
63
load: null, // onLoad callback
64
params: null, // object init params
65
context: null // callback arg passed to the load callback
66
};
67
68
$.fn.silverlight.counter = 0;
69
70
})(jQuery);
/** 02
* jQuery Plugin for use with Microsoft Silverlight 03
* Tested with Silverlight 1.0 beta 04
* @author: M. Alsup (malsup at gmail dot com) 05
* @version: 1.0 (5/04/2007) 06
* Documentation and examples at: http://www.malsup.com/jquery/ag/ 07
* Free beer and free speech. Enjoy! 08
*/ 09
(function($) { 10
11
// CHANGE FOR SILVERLIGHT 2.0 12
if (!window || !window.Silverlight) { 13
alert('You must include the Silverlight.js script.'); 14
return; 15
} 16
17
$.fn.silverlight = function(opts) { 18
return this.each(function() { 19
var o = jQuery.extend({}, $.fn.silverlight.defaults, opts); 20
21
// try to use metadata plugin 22
if ($.meta) jQuery.extend(o, $(this).data()); 23
24
// make sure activex object gets a unique id 25
var id = o.id || (this.id ? this.id + 'AG' : 'AG' + $.fn.silverlight.counter++); 26
var props = { 27
width: o.width, 28
height: o.height, 29
inplaceInstallPrompt: o.installPrompt, 30
background: o.bg, 31
isWindowless: o.windowless, 32
framerate: o.framerate, 33
version: o.version 34
}; 35
var events = { 36
onError: o.error, 37
onLoad: o.load 38
}; 39
40
// CHANGE FOR SILVERLIGHT 2.0 41
Silverlight.createObjectEx({ 42
source: o.xaml, 43
parentElement: this, 44
id: id, 45
properties: props, 46
events: events, 47
initParams: o.params, 48
userContext: o.context 49
}); 50
}); 51
}; 52
53
// @see http://msdn2.microsoft.com/en-us/library/bb190632.aspx 54
$.fn.silverlight.defaults = { 55
width: '300', // width of component in px 56
height: '300', // height of component in px 57
bg: '#00000000', // background color (default is transparent) 58
installPrompt: 'true', // display in-place install prompt? 59
windowless: 'true', // windowless mode (false for wrapping markup) 60
framerate: '24', // maximum framerate 61
version: '0.9', // Silverlight version 62
error: null, // onError callback 63
load: null, // onLoad callback 64
params: null, // object init params 65
context: null // callback arg passed to the load callback 66
}; 67
68
$.fn.silverlight.counter = 0; 69
70
})(jQuery);PS: 此程式著作權為 M. Alsup (malsup at gmail dot com) 所有,本文轉載僅供修改的說明之用,特此註明。
(function($)
if (!window || !window.Silverlight)