(一).概述
(二). 代码示例
1).基础知识示例
1. 对象的定位
2. 使用SilverLight插件
3. SilverLight元素的鼠标事件
4. SilverLight元素的键盘事件
5. 方法createFromXaml使用示例
6. 访问和修改Silverlight元素以及属性
7. Silverlight中的字体使用
8. 全屏支持功能
9. Silverlight Downloader 对象使用
10.定时器功能
11.错误处理支持
12.把xaml段内置在aspx或html页面中
2).制作图形示例
13.基本图形形状示例
14.画刷对象的使用
15.视频刷对象的使用
16.Path元素使用
17.变换效果(Transform)
3).多媒体功能
18.在线电影播放
4).制作动画效果示例
19.一个动画示例
20.动画的交互控制
21.动画关键帧示例
(二). 代码示例
1).基础知识示例
1. 对象的定位
2. 使用SilverLight插件
3. SilverLight元素的鼠标事件
4. SilverLight元素的键盘事件
5. 方法createFromXaml使用示例
6. 访问和修改Silverlight元素以及属性
7. Silverlight中的字体使用
8. 全屏支持功能
9. Silverlight Downloader 对象使用
10.定时器功能
11.错误处理支持
12.把xaml段内置在aspx或html页面中
2).制作图形示例
13.基本图形形状示例
14.画刷对象的使用
15.视频刷对象的使用
16.Path元素使用
17.变换效果(Transform)
3).多媒体功能
18.在线电影播放
4).制作动画效果示例
19.一个动画示例
20.动画的交互控制
21.动画关键帧示例
Silverlight是一个跨浏览器的、跨操作系统平台的插件,为Web带来下一代媒体体验和丰富的交互式应用程序体验. 使用JS实现Ajax技术可以使网站具有很好的用户体验, 但它也会使网站过于庞大和臃肿. 业余时间学习了一下微软的Silverlight官方教程, 在学习时顺便根据学习示例整理了一个教程. 此教程包括一些精简的小例子, 主要针对开发使用场景展开示例, 在实际开发中也可以作为查找手册使用. 运行环境为VS 2005, 这些示例比较简单很容易看懂, 因此示例没有太多注释.
(二). 代码示例
1).基础知识示例
1. 对象的定位
通过呈现几个Silverlight元素来示例对象之间的定位, 以及在一个页面中创建装载多个Silverlight对象.
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Silverlight 对象的定位</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="SilverlightControlHost1" >
<script type="text/javascript">
createSilverlight1();
</script>
</div>
<hr>
<div id="SilverlightControlHost2" >
<script type="text/javascript">
createSilverlight2();
</script>
</div>
<hr>
<div id="SilverlightControlHost3" >
<script type="text/javascript">
createSilverlight3();
</script>
</div>
<hr>
<div id="SilverlightControlHost4" >
<script type="text/javascript">
createSilverlight4();
</script>
</div>
<hr>
<div id="SilverlightControlHost5" >
<script type="text/javascript">
createSilverlight5();
</script>
</div>
</form>
</body>
</html>
II. 页面对应的JS文件代码(Default.aspx.js)
//创建对象
function createSilverlight1()
{
Silverlight.createObjectEx({
source: "Page1.xaml",
parentElement: document.getElementById("SilverlightControlHost1"),
id: "SilverlightControl1",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function createSilverlight2()
{
Silverlight.createObjectEx({
source: "Page2.xaml",
parentElement: document.getElementById("SilverlightControlHost2"),
id: "SilverlightControl2",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function createSilverlight3()
{
Silverlight.createObjectEx({
source: "Page3.xaml",
parentElement: document.getElementById("SilverlightControlHost3"),
id: "SilverlightControl3",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function createSilverlight4()
{
Silverlight.createObjectEx({
source: "Page4.xaml",
parentElement: document.getElementById("SilverlightControlHost4"),
id: "SilverlightControl4",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function createSilverlight5()
{
Silverlight.createObjectEx({
source: "Page5.xaml",
parentElement: document.getElementById("SilverlightControlHost5"),
id: "SilverlightControl5",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
III.Xaml文件代码
1. Page1.xaml代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640"Height="480"
Background="LightGreen"
Canvas.Left="10"
Canvas.Top="10"
>
<RectangleCanvas.Left="30"Canvas.Top="30"Fill="LightBlue"
Width="200"Height="200" />
</Canvas>
2. Page2.xaml代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300"Height="300"
Background="White">
<CanvasWidth="250"Height="250"Canvas.Left="30"Canvas.Top="30"Background="LightBlue">
<RectangleCanvas.Left="30"Canvas.Top="30"Fill="LightGreen"
Width="200"Height="200" />
</Canvas>
</Canvas>
3. Page3.xaml代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300"Height="300"
Background="White">
<LineX1="280"Y1="10"X2="10"Y2="280"
Stroke="Blue"StrokeThickness="5"/>
<PolylinePoints="150, 150 150, 250 250, 250 250, 150"
Stroke="Yellow"StrokeThickness="10"/>
<PathData="M 10,100 C 10,300 300,-200 250,100z"
Stroke="Red"Fill="Orange"
Canvas.Left="10"Canvas.Top="10" />
</Canvas>
4. Page4.xaml代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200"Height="200">
<Rectangle
Canvas.Left="100"Canvas.Top="100"
Width="50"Height="50"
Fill="RoyalBlue">
<Rectangle.RenderTransform>
<RotateTransformAngle="45" />
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
5. Page5.xaml代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Ellipse
Canvas.ZIndex="3"
Canvas.Left="5"Canvas.Top="5"
Height="200"Width="200"
Stroke="Black"StrokeThickness="10"Fill="Silver" />
<Ellipse
Canvas.ZIndex="2"
Canvas.Left="50"Canvas.Top="50"
Height="200"Width="200"
Stroke="Black"StrokeThickness="10"Fill="DeepSkyBlue" />
<Ellipse
Canvas.ZIndex="1"
Canvas.Left="95"Canvas.Top="95"
Height="200"Width="200"
Stroke="Black"StrokeThickness="10"Fill="Lime" />
</Canvas>
|
2. 使用SilverLight插件
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>访问Silverlight插件</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
Opacity="0"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"MouseLeftButtonDown="MouseLeftButtonDown"
>
<Canvas.Triggers>
<EventTriggerRoutedEvent="Canvas.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationStoryboard.TargetName="parentCanvas"Storyboard.TargetProperty="Opacity"From="0"To="1"Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Canvas.Triggers>
<TextBlockName="helloBlock"Foreground="Black"Cursor="Hand"Text="King" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {onLoad:onLoad}
});
}
//判断当前安装插件的版本
function onLoaded(sender, eventArgs)
{
var version = "1.1";
var isInstalled = Silverlight.isInstalled(version);
alert(isInstalled);
}
//访问运行中插件
function MouseLeftButtonDown(sender, mouseEventArgs)
{
var plugin = sender.getHost();
alert("Silverlight V1.1 supported: " + plugin.isVersionSupported("1.1"));//plugin
alert("IsLoaded: " + plugin.IsLoaded);//plugin
alert("Background: " + plugin.Settings.Background); //plugin.Settings
alert("ActualHeight: " + plugin.Content.ActualHeight );//plugin.Content
}
function onLoad(plugin, userContext, sender)
{
alert(plugin.id + " : " + userContext + " : " + sender.toString());
}
|
3. SilverLight元素的鼠标事件
I.鼠标滑入和滑出事件
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Silverlight元素的鼠标进入/离开事件</title>
<script type="text/javascript" src="..\..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded">
<Ellipse
MouseEnter="onMouseEnter"
MouseLeave="onMouseLeave"
Height="100"Width="100"
Fill="Teal"Name="Ellispse1" />
<Ellipse
MouseEnter="onMouseEnter"
MouseLeave="onMouseLeave"
Canvas.Left="120"
Height="100"Width="100"
Fill="Teal"Name="Ellispse2" />
<TextBlockName="Status"Canvas.Top="120" />
<TextBlockName="RemoveClickEvent"Canvas.Top="250"Text="Click Me Remove Mouse Click Event"MouseLeftButtonDown="onMouseDownRemoveEvent" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onMouseEnter(sender, mouseEventArgs)
{
sender.fill = "Coral";
}
function onMouseLeave(sender, eventArgs)
{
sender.fill = "Teal";
}
var entertoken1, entertoken2,entertoken1,entertoken2;
function onLoaded(sender, eventArgs)
{
Ellispse1 = sender.findName("Ellispse1");
Ellispse2 = sender.findName("Ellispse2");
entertoken1 = Ellispse1.addEventListener("MouseLeftButtonUp", onMouseLeftButtonUp);
entertoken2 = Ellispse2.addEventListener("MouseLeftButtonUp", onMouseLeftButtonUp);
}
function onMouseLeftButtonUp(sender, mouseEventArgs)
{
var pt = mouseEventArgs.getPosition(null);
sender.findName("Status").text = "(" + pt.x + "," + pt.y + ")";
}
function onMouseDownRemoveEvent(sender, mouseEventArgs)
{
Ellispse1.removeEventListener("MouseLeftButtonUp", entertoken1);
Ellispse2.removeEventListener("MouseLeftButtonUp", entertoken2);
}
|
II.鼠标点击(按下/弹起)和移动事件
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Silverlight元素的鼠标点击/移动事件</title>
<script type="text/javascript" src="..\..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvasxmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MouseLeftButtonDown="onMouseDown"
MouseLeftButtonUp="onMouseUp"
MouseMove="onMouseMove"
Canvas.Top="20"Canvas.Left="50">
<Ellipse
Height="100"Width="100"
Fill="Gold"
Stroke="Black"StrokeThickness="4" />
<Ellipse
Height="50"Width="50"
Canvas.Top="25"Canvas.Left="25"
Fill="Black" />
<Canvas
MouseLeftButtonDown="onMouseDown"
MouseLeftButtonUp="onMouseUp"
MouseMove="onMouseMove"
Canvas.Top="140"Canvas.Left="100">
<Canvas.RenderTransform>
<RotateTransformAngle="45" />
</Canvas.RenderTransform>
<Rectangle
Height="100"Width="100"
Fill="Coral"
Stroke="Black"StrokeThickness="4" />
</Canvas>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
var beginX;
var beginY;
var isMouseDown = false;
function onMouseDown(sender, mouseEventArgs)
{
beginX = mouseEventArgs.getPosition(null).x;
beginY = mouseEventArgs.getPosition(null).y;
isMouseDown = true;
//使当前控件获取鼠标焦点, 即使光标不在该元素上
sender.captureMouse();
}
function onMouseMove(sender, mouseEventArgs)
{
if (isMouseDown == true)
{
var currX = mouseEventArgs.getPosition(null).x;
var currY = mouseEventArgs.getPosition(null).y;
sender["Canvas.Left"] += currX - beginX;
sender["Canvas.Top"] += currY - beginY;
beginX = currX;
beginY = currY;
}
}
function onMouseMove(sender, mouseEventArgs)
{
if (isMouseDown == true)
{
var currX = mouseEventArgs.getPosition(null).x;
var currY = mouseEventArgs.getPosition(null).y;
sender["Canvas.Left"] += currX - beginX;
sender["Canvas.Top"] += currY - beginY;
beginX = currX;
beginY = currY;
}
}
function onMouseUp(sender, mouseEventArgs)
{
isMouseDown = false;
sender.releaseMouseCapture();
}
|
4. SilverLight元素的键盘事件
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Canvas键盘按键事件</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
KeyDown="onKeyDown"
>
<TextBlockName="helloBlock"Foreground="Black"Cursor="Hand"Text="King" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onKeyDown(sender, keyEventArgs)
{
var msg;
msg = " Press Shift = " + keyEventArgs.shift;
msg += " Press Ctrl = " + keyEventArgs.ctrl;
msg += " Key = " + keyEventArgs.key;
msg += " PlatformKeyCode = " + keyEventArgs.PlatformKeyCode;
msg += " The Platform is windows = " + (navigator.userAgent.indexOf("Windows") != -1);
alert(msg);
}
|
5.方法createFromXaml使用示例
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>createFromXaml方法使用示例</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MouseLeftButtonDown="onMouseLeftButtonDown"
Width="100"Height="100"Background="LightBlue"
>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onMouseLeftButtonDown(sender, eventArgs)
{
var plugin = sender.getHost();
var xamlFragment = '<TextBlock Canvas.Top="0" Text="I was created by createFromXaml method!" />';
textBlock = plugin.content.createFromXaml(xamlFragment);
var xamlFragment2 = '<Ellipse Canvas.ZIndex="1" Canvas.Left="95" Canvas.Top="95" Height="200" Width="200" Stroke="Black" StrokeThickness="10" Fill="Lime" />';
ellipse = plugin.content.createFromXaml(xamlFragment2);
ellipse.fill = createLinearGradientBrush(plugin);
sender.children.add(textBlock);
sender.children.add(ellipse);
}
function createLinearGradientBrush(plugin)
{
var xamlFragment = '<LinearGradientBrush>';
xamlFragment += '<GradientStop Color="Yellow" Offset="0.0" />';
xamlFragment += '<GradientStop Color="Orange" Offset="0.5" />';
xamlFragment += '<GradientStop Color="Red" Offset="1.0" />';
xamlFragment += '</LinearGradientBrush>';
return plugin.content.createFromXaml(xamlFragment);
}
|
6.访问和修改Silverlight元素以及属性
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>访问和修改Silverlight元素以及属性</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<TextBlockName="TextBlock1"Foreground="Black"Cursor="Hand" />
<TextBlockName="TextBlock2"Foreground="Black"Cursor="Hand"Canvas.Top="20" />
<TextBlockName="TextBlock3"Foreground="Black"Cursor="Hand"Canvas.Top="40" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
//findName Method
sender.findName("TextBlock1").text = "King1";
//getItem Method
sender.children.getItem(1).text = "King2";
//getValue 和setValue 方法
var textBlock3 = sender.findName("TextBlock3");
textBlock3.text = "King3";
var text = textBlock3.getValue("text");
textBlock3.setValue("text", text);
//Extend Property
textBlock3["Canvas.Top"] = 40; //自己修改一下该值
}
|
7. Silverlight中的字体使用
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Silverlight中的字体</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<TextBlock
Text="Font Attributes"
FontFamily="Verdana"
FontSize="36"
FontStretch="UltraExpanded"
FontStyle="Italic"
FontWeight="ExtraBlack" />
<!-- Run 对象是一个文字元素 -->
<TextBlockCanvas.Top="60"
FontFamily="Arial"Width="400"Text="Sample text formatting runs">
<LineBreak/>
<RunForeground="Maroon"FontFamily="Courier New"FontSize="24">Courier New 24</Run>
<LineBreak/>
<RunForeground="Teal"FontFamily="Times New Roman"FontSize="18"FontStyle="Italic">Times New Roman Italic 18</Run>
<LineBreak/>
<RunForeground="SteelBlue"FontFamily="Verdana"FontSize="14"FontWeight="Bold">Verdana Bold 14</Run>
</TextBlock>
<TextBlock
Canvas.Top="150"
FontFamily="Arial"
FontSize="32"
FontWeight="Bold"
Foreground="Navy">
Navy
<RunText="DodgerBlue "Foreground="DodgerBlue"/>
<RunText="LightSteelBlue "Foreground="LightSteelBlue"/>
</TextBlock>
<TextBlock
Canvas.Top="200"
FontFamily="Verdana"
FontSize="32"
FontWeight="Bold">
LINEAR GRADIENT BRUSH
<TextBlock.Foreground>
<LinearGradientBrushStartPoint="0,0"EndPoint="1,1">
<GradientStopColor="Red"Offset="0.0" />
<GradientStopColor="Orange"Offset="0.2" />
<GradientStopColor="Yellow"Offset="0.4" />
<GradientStopColor="Green"Offset="0.6" />
<GradientStopColor="Blue"Offset="0.8" />
<GradientStopColor="Violet"Offset="1.0" />
</LinearGradientBrush>
</TextBlock.Foreground>
<TextBlock.RenderTransform>
<ScaleTransformScaleY="3.0" />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlockText="Rotated Text"FontSize="32"Foreground="Teal"Canvas.Top="300">
<TextBlock.RenderTransform>
<RotateTransformAngle="90" />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlockCanvas.Top="400"
FontSize="32"
FontWeight="Bold"
Foreground="Maroon"
Text="Skewed Text">
<TextBlock.RenderTransform>
<SkewTransformAngleX="-30"AngleY="0" />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Canvas.Top="450"
FontSize="32"
FontWeight="Bold"
Foreground="Maroon"
Text="Skewed Text">
<TextBlock.RenderTransform>
<SkewTransformAngleX="30"AngleY="0" />
</TextBlock.RenderTransform>
</TextBlock>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "1000",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
}
|
8.全屏支持功能
I. 页面代码(Default.aspx)
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>全屏支持功能</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300"Height="300"Loaded="onLoaded">
<MediaElementName="media"Source="http://jd.ikmtv.com/mymtv123/许志安_光辉岁月.wmv"Width="300"Height="300"MouseLeftButtonUp="onMouseLeftButtonUp" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "500",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
//双击事件
function onMouseLeftButtonUp(sender, mouseEventArgs)
{
//plugin.content.fullScreen = !plugin.content.fullScreen;
var b=(new Date).getTime(),c=this._timeLastLeftButtonUp;
this._timeLastLeftButtonUp=b;
var a=c&&b-c<300;
if(a)
{
//处理双击事件
this._timeLastLeftButtonUp=0;
var plugin=sender.getHost();
plugin.content.fullScreen = !plugin.content.fullScreen;
}
else
{
//处理单击事件
}
}
var media;
function onLoaded(sender, mouseEventArgs)
{
media = sender.findName("media");
//处理FullScreenChangeded事件
plugin = sender.getHost();
plugin.content.onFullScreenChange = onFullScreenChanged;
updateLayout(plugin.content.actualWidth, plugin.content.actualHeight);
}
function onFullScreenChanged(sender, eventArgs)
{
updateLayout(plugin.content.actualWidth, plugin.content.actualHeight);
}
function updateLayout(width, height)
{
media.width=width;
media.height=height;
}
|
9. Silverlight Downloader 对象使用
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Silverlight Downloader 对象使用</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<CanvasMouseLeftButtonDown="onMouseLeftButtonDown"Canvas.Left="10">
<RectangleStroke="Black"Height="30"Width="250"RadiusX="5"RadiusY="5">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="Orange"Offset="0.0" />
<GradientStopColor="Red"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="10"Canvas.Top="5">Click me to download a picture!</TextBlock>
</Canvas>
<!--下载进度条 -->
<CanvasCanvas.Top="70">
<RectangleName="progressRectangle"
Canvas.Left="20"
Height="10"Width="0"
Fill="Maroon" />
<Rectangle
Canvas.Top ="-1"
Canvas.Left="19"Height="12"
Width="202"
StrokeThickness="1"Stroke="Black" />
<TextBlockName="progressText"
Canvas.Top ="-4"Canvas.Left="230"
Text="0%"FontSize="12" />
</Canvas>
<TextBlock Canvas.Top="100"Width="100"Height="100"FontSize="72">
King,King,King
<TextBlock.Foreground>
<ImageBrushName="imageBrush" />
</TextBlock.Foreground>
</TextBlock>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
progressText = sender.findName("progressText");
progressRectangle = sender.findName("progressRectangle");
imageBrush = sender.findName("imageBrush");
}
function onMouseLeftButtonDown(sender, eventArgs)
{
var slPlugin = sender.getHost();
var downloader = slPlugin.createObject("downloader");
downloader.addEventListener("downloadProgressChanged", onDownloadProgressChanged);
downloader.addEventListener("completed", onCompleted);
downloader.open("GET", "Silverlight_Multimedea.JPG");
downloader.send();
}
function onDownloadProgressChanged(sender, eventArgs)
{
var percentage = Math.floor(sender.downloadProgress * 100);
progressText.text = percentage + "%";
progressRectangle.width = percentage * 2;
}
function onCompleted(sender, eventArgs)
{
//var xamlFragment = sender.ResponseText;
//var button = plugin.content.createFromXaml(xamlFragment);
var plugin = sender.getHost();
var imageBrush = sender.findName("imageBrush");
imageBrush.setSource(sender,"");
}
|
10.定时器功能
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>定时器功能</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas.Resources>
<!--时间故事版 -->
<StoryboardDuration="0:0:1"Completed="StoryboardFinished"x:Name="timerStoryboard" />
</Canvas.Resources>
<Rectangle
Name="MyAnimatedRectangle"
Width="100"
Height="100"
Fill="Blue"
Loaded="StartTimer">
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0"To="0"Duration="0:0:5"
AutoReverse="True"RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
<TextBlockCanvas.Top="110"Name="myTextBlock" ></TextBlock>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
}
function StartTimer(sender, mouseEventArgs)
{
var timer = sender.findName("timerStoryboard");
timer.begin();
}
function StoryboardFinished(sender, mouseEventArgs)
{
var rect = sender.findName("MyAnimatedRectangle");
var txtBlock = sender.findName("myTextBlock");
txtBlock.text = String(rect.opacity);
var timer = sender.findName("timerStoryboard");
timer.begin();
}
|
11.错误处理支持
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>错误处理支持</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded">
<Ellipse
MouseLeftButtonDown="onMouseDownEventHandler"
Height="200"Width="200"
Fill="Teal"Name="Ellispse1" />
<MediaElementName="media"Width="300"Height="300"MediaFailed="MediaFailedHandler" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events:
{onError:onErrorandler}
});
}
function onMouseDownEventHandler(sender, moseEventArgs)
{
var player = sender.findName("media");
//方式
//player.play();
//return;
//方式
try
{
player.play();
}
catch(e)
{
alert(e.message);
}
}
//方式
//注意: events:{onError:onErrorandler} 中onError:null时将执行此代码
function MediaFailedHandler(sender, args)
{
var errorMsg = "\n Media Error Message \n" ;
errorMsg += "MediaElement Name: " + sender.Name + "\n";
errorMsg += "Media File Name: " + sender.Source + "\n";
alert(errorMsg);
}
//events:{onError:onErrorandler}
function onErrorandler(sender, errorArgs)
{
var errorMsg = "Silverlight Error: \n\n";
errorMsg += "Error Type: " + errorArgs.errorType + "\n";
errorMsg += "Error Message: " + errorArgs.errorMessage + "\n";
errorMsg += "Error Code: " + errorArgs.errorCode + "\n";
switch(errorArgs.errorType)
{
case "RuntimeError":
if (errorArgs.lineNumber != 0)
{
errorMsg += "Line: " + errorArgs.lineNumber + "\n";
errorMsg += "Position: " + errorArgs.charPosition + "\n";
}
errorMsg += "MethodName: " + errorArgs.methodName + "\n";
break;
case "ParserError":
errorMsg += "Xaml File: " + errorArgs.xamlFile + "\n";
errorMsg += "Xml Element: " + errorArgs.xmlElement + "\n";
errorMsg += "Xml Attribute: " + errorArgs.xmlAttribute + "\n";
errorMsg += "Line: " + errorArgs.lineNumber + "\n";
errorMsg += "Position: " + errorArgs.charPosition + "\n";
break;
default:
break;
}
alert(errorMsg);
}
|
12.把xaml段内置在aspx或html页面中
这样的话就不用单独的 *.xaml 文件来承载SilverLight对象控件元素内容.
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>把xaml段内置在aspx或html页面中</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<!-- 保证这段代码在createSilverlight方法上面 -->
<script type="text/xaml" id="xamlContent">
<?xml version="1.0"?>
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
Background="Wheat">
<TextBlock
Canvas.Left="20"
FontSize="24"
Loaded="setDate" />
</Canvas>
</script>
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
文件省略, 其内容集成到 Default.aspx页面中了.
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: '#xamlContent',
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function setDate(sender, eventArgs)
{
sender.text = Date();
}
|
2).制作图形示例
13.基本图形形状示例
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>基本图形形状示例</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<!--预览一些图形 -->
<!--画一条线段, 从 (10,10) 到 (50,50). -->
<Line
X1="10"Y1="10"
X2="50"Y2="50"
Stroke="Black"
StrokeThickness="4" />
<!--画一条线段, 从 (10,10) 到 (50,50). 并且横坐标往右移动100个象素 -->
<Line
X1="10"Y1="10"
X2="50"Y2="50"
StrokeThickness="4"
Canvas.Left="100">
<Line.Stroke>
<RadialGradientBrushGradientOrigin="0.5,0.5"Center="0.5,0.5"RadiusX="0.5"RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStopColor="Red"Offset="0" />
<GradientStopColor="Blue"Offset="0.25" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Line.Stroke>
</Line>
<!--画一条水平线, 从坐标 (10,60) 到 (150,60). -->
<Line
X1="10"Y1="60"
X2="150"Y2="60"
Stroke="Black"
StrokeThickness="4"/>
<!--画一个椭圆. -->
<Ellipse
Fill="Yellow"
Height="100"
Width="200"
StrokeThickness="2"
Stroke="Black"Canvas.Top="100"/>
<!--一条路径 -->
<PathStroke="DarkGoldenRod"StrokeThickness="3"
Data="M 100,200 C 100,25 400,350 400,175 H 280"Canvas.Top="150" />
<!--多边形. -->
<PolylinePoints="25,25 0,50 25,75 50,50 25,25 25,0"
Stroke="Blue"StrokeThickness="10"
Canvas.Left="75"Canvas.Top="250">
<Polyline.RenderTransform>
<RotateTransformCenterX="0"CenterY="0"Angle="45" />
</Polyline.RenderTransform>
</Polyline>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
}
|
14.画刷对象的使用
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>画刷</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<CanvasName="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<RectangleWidth="100"Height="100"Fill="Red" />
<RectangleWidth="100"Height="100"Fill="#FFFF0000"Canvas.Top="110" />
<RectangleWidth="100"Height="100"Canvas.Top="220" >
<Rectangle.Fill>
<SolidColorBrushColor="Red" />
</Rectangle.Fill>
</Rectangle>
<RectangleWidth="100"Height="100"Canvas.Top="330">
<Rectangle.Fill>
<SolidColorBrushColor="#FFFF0000" />
</Rectangle.Fill>
</Rectangle>
<RectangleWidth="200"Height="100"Canvas.Top="440">
<Rectangle.Fill>
<LinearGradientBrushStartPoint="0,0"EndPoint="1,1">
<GradientStopColor="Yellow"Offset="0.0" />
<GradientStopColor="Red"Offset="0.25" />
<GradientStopColor="Blue"Offset="0.75" />
<GradientStopColor="LimeGreen"Offset="1.0" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<RectangleWidth="200"Height="100"Canvas.Top="550">
<Rectangle.Fill>
<LinearGradientBrushStartPoint="0,0.5"EndPoint="1,0.5">
<GradientStopColor="Yellow"Offset="0.0" />
<GradientStopColor="Red"Offset="0.25" />
<GradientStopColor="Blue"Offset="0.75" />
<GradientStopColor="LimeGreen"Offset="1.0" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<RectangleWidth="200"Height="100"Canvas.Top="660">
<Rectangle.Fill>
<LinearGradientBrushStartPoint="0.5,0"EndPoint="0.5,1">
<GradientStopColor="Yellow"Offset="0.0" />
<GradientStopColor="Red"Offset="0.25" />
<GradientStopColor="Blue"Offset="0.75" />
<GradientStopColor="LimeGreen"Offset="1.0" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<RectangleWidth="200"Height="100"Canvas.Top="770">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.5,0.5"Center="0.5,0.5"
RadiusX="0.5"RadiusY="0.5">
<GradientStopColor="Yellow"Offset="0" />
<GradientStopColor="Red"Offset="0.25" />
<GradientStopColor="Blue"Offset="0.75" />
<GradientStopColor="LimeGreen"Offset="1" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<RectangleWidth="100"Height="100"Canvas.Top="880">
<Rectangle.Fill>
<LinearGradientBrushStartPoint="0,0">
<GradientStopColor="#200000FF"Offset="0.0" />
<GradientStopColor="#FF0000FF"Offset="1.0" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "10000",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
}
|
15.视频刷对象的使用
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>视频刷使用</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<MediaElement
Name="mediaElement"
Source="http://jd.ikmtv.com/beyond_ghsy.wmv"IsMuted="True"
Opacity="0.0" />
<TextBlockCanvas.Left="5"Canvas.Top="30"
FontFamily="Verdana"FontSize="120"
FontWeight="Bold"TextWrapping="Wrap"
Text="Video">
<TextBlock.Foreground>
<VideoBrushSourceName="mediaElement"Stretch="UniformToFill" />
</TextBlock.Foreground>
</TextBlock>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function onLoaded(sender, eventArgs)
{
}
|
16.Path元素使用
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Path元素使用</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<PathStroke="Black"Fill="Gray"
Data="M 10,100 C 10,300 300,-200 300,100" />
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
|
17.变换效果(Transform)
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>变换效果</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200"Height="200">
<Rectangle
Canvas.Left="100"Canvas.Top="100"
Width="50"Height="50"
Fill="RoyalBlue">
<Rectangle.RenderTransform>
<RotateTransformAngle="45" />
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle
Canvas.Left="100"Canvas.Top="200"
Width="50"Height="50"
Fill="RoyalBlue">
<Rectangle.RenderTransform>
<RotateTransformAngle="45"CenterX="25"CenterY="25" />
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle
Canvas.Left="100"Canvas.Top="300"
Width="50"Height="50"
Fill="RoyalBlue">
<Rectangle.RenderTransform>
<RotateTransformName="myTransform"Angle="45"CenterX="25"CenterY="25" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="myTransform"
Storyboard.TargetProperty="Angle"
From="0"To="360"Duration="0:0:5"RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "500",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
|
3).多媒体功能
18.在线电影播放
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>在线影视播放</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300"Name="mainControl"Height="300"Loaded="onLoaded">
<MediaElementName="media"Source="http://jd.ikmtv.com/aymtv/beyond_喜欢你.wmv"Width="300"Height="300"
MouseLeftButtonUp="onMouseLeftButtonUp"AutoPlay="True"Visibility="Visible" />
<!--停止 -->
<CanvasMouseLeftButtonDown="media_stop"
Canvas.Left="0"Canvas.Top="310">
<RectangleStroke="Black"
Height="30"Width="55"RadiusX="5"RadiusY="5">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="LimeGreen"Offset="0.0" />
<GradientStopColor="Green"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="5"Canvas.Top="5">Stop</TextBlock>
</Canvas>
<!--暂停 -->
<CanvasMouseLeftButtonDown="media_pause"
Canvas.Left="60"Canvas.Top="310">
<RectangleStroke="Black"
Height="30"Width="55"RadiusX="5"RadiusY="5">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="LimeGreen"Offset="0.0" />
<GradientStopColor="Green"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="5"Canvas.Top="5">Pause</TextBlock>
</Canvas>
<!--播放 -->
<CanvasMouseLeftButtonDown="media_begin"
Canvas.Left="120"Canvas.Top="310">
<RectangleStroke="Black"RadiusX="5"RadiusY="5"
Height="30"Width="55">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="LimeGreen"Offset="0.0" />
<GradientStopColor="Green"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="5"Canvas.Top="5">Play</TextBlock>
</Canvas>
<!--加音 -->
<CanvasMouseLeftButtonDown="media_addvolumn"
Canvas.Left="180"Canvas.Top="310">
<RectangleStroke="Black"RadiusX="5"RadiusY="5"
Height="30"Width="55">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="LimeGreen"Offset="0.0" />
<GradientStopColor="Green"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="5"Canvas.Top="5">+V</TextBlock>
</Canvas>
<!--哑音 -->
<CanvasMouseLeftButtonDown="media_mutevolumn"
Canvas.Left="240"Canvas.Top="310">
<RectangleStroke="Black"RadiusX="5"RadiusY="5"
Height="30"Width="55">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="LimeGreen"Offset="0.0" />
<GradientStopColor="Green"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="5"Canvas.Top="5"> No V</TextBlock>
</Canvas>
<!--减音 -->
<CanvasMouseLeftButtonDown="media_subvolumn"
Canvas.Left="300"Canvas.Top="310">
<RectangleStroke="Black"RadiusX="5"RadiusY="5"
Height="30"Width="50">
<Rectangle.Fill>
<RadialGradientBrushGradientOrigin="0.75,0.25">
<GradientStopColor="LimeGreen"Offset="0.0" />
<GradientStopColor="Green"Offset="1.0" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlockCanvas.Left="5"Canvas.Top="5"> -V</TextBlock>
</Canvas>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function media_stop(sender, args) {
sender.findName("media").stop();
}
function media_pause(sender, args) {
sender.findName("media").pause();
}
function media_begin(sender, args) {
sender.findName("media").play();
}
function media_addvolumn(sender, args) {
sender.findName("media").Volume += 0.1;
}
function media_subvolumn(sender, args) {
sender.findName("media").Volume -= 0.1;
}
function media_mutevolumn(sender, args) {
sender.findName("media").IsMuted = !sender.findName("media").IsMuted;
}
//双击事件
function onMouseLeftButtonUp(sender, mouseEventArgs)
{
//plugin.content.fullScreen = !plugin.content.fullScreen;
var b=(new Date).getTime(),c=this._timeLastLeftButtonUp;
this._timeLastLeftButtonUp=b;
var a=c&&b-c<300;
if(a)
{
//处理双击事件
this._timeLastLeftButtonUp=0;
var plugin=sender.getHost();
plugin.content.fullScreen = !plugin.content.fullScreen;
}
else
{
//处理单击事件
}
}
var media;
function onLoaded(sender, mouseEventArgs)
{
media = sender.findName("media");
//处理FullScreenChangeded事件
plugin = sender.getHost();
plugin.content.onFullScreenChange = onFullScreenChanged;
updateLayout(plugin.content.actualWidth, plugin.content.actualHeight);
}
function onFullScreenChanged(sender, eventArgs)
{
updateLayout(plugin.content.actualWidth, plugin.content.actualHeight);
}
function updateLayout(width, height)
{
media.width=width;
media.height=height;
}
|
4).制作动画效果示例
19.一个动画示例
随着时间背景产生渐变效果的矩形.
I.页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>随着时间背景产生渐变效果的矩形</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Rectangle
Name="MyAnimatedRectangle"
Width="100"
Height="100"
Fill="Blue">
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0"To="0.0"Duration="0:0:5"AutoReverse="True"RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
|
20.动画的交互控制
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>动画的交互控制</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas.Resources>
<StoryboardName="myStoryboard">
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0"To="0.0"Duration="0:0:5"
AutoReverse="True" />
</Storyboard>
</Canvas.Resources>
<Rectangle
Name="MyAnimatedRectangle"
Width="100"
Height="100"
Fill="Blue"
MouseLeftButtonDown="startAnimation">
</Rectangle>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "350",
height: "350",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
function startAnimation(sender, mouseEventArgs)
{
//开始运行
sender.findName("myStoryboard").begin();
}
|
21.动画关键帧示例
I. 页面代码(Default.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>动画的关键帖</title>
<script type="text/javascript" src="..\Silverlight.js"></script>
<script type="text/javascript" src="Default.aspx.js"></script>
</head>
<body onload="document.getElementById('SilverlightControlHost').focus()">
<form id="form1" runat="server">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</form>
</body>
</html>
II.Xaml文件代码
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400"Height="300">
<RectangleFill="Blue"
Width="50"Height="50">
<Rectangle.RenderTransform>
<TranslateTransform
Name="MyAnimatedTranslateTransform1"
X="0"Y="0" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="MyAnimatedTranslateTransform1"
Storyboard.TargetProperty="X"
Duration="0:0:10">
<LinearDoubleKeyFrameValue="0"KeyTime="0:0:0" />
<LinearDoubleKeyFrameValue="350"KeyTime="0:0:2" />
<LinearDoubleKeyFrameValue="50"KeyTime="0:0:7" />
<LinearDoubleKeyFrameValue="200"KeyTime="0:0:8" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
<RectangleFill="Blue"
Width="50"Height="50">
<Rectangle.RenderTransform>
<TranslateTransform
Name="SplineAnimatedTranslateTransform2"
X="0"Y="0" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="SplineAnimatedTranslateTransform2"
Storyboard.TargetProperty="X"
Duration="0:0:15"
RepeatBehavior="Forever">
<SplineDoubleKeyFrameValue="500"
KeyTime="0:0:7"KeySpline="0.0,1.0 1.0,0.0" />
<SplineDoubleKeyFrameValue="200"
KeyTime="0:0:10"KeySpline="0.0,0.0 1.0,0.0" />
<SplineDoubleKeyFrameValue="350"
KeyTime="0:0:15"KeySpline="0.25,0.5 0.75,1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
<RectangleFill="Blue"
Width="50"Height="50">
<Rectangle.RenderTransform>
<TranslateTransform
Name="ComboAnimatedTranslateTransform3"
X="0"Y="0" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="ComboAnimatedTranslateTransform3"
Storyboard.TargetProperty="X"
Duration="0:0:15"
RepeatBehavior="Forever">
<DiscreteDoubleKeyFrameValue="500"KeyTime="0:0:7" />
<LinearDoubleKeyFrameValue="200"KeyTime="0:0:10" />
<SplineDoubleKeyFrameValue="350"KeyTime="0:0:15"
KeySpline="0.25,0.5 0.75,1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
<RectangleFill="Blue"
Width="50"Height="50">
<Rectangle.RenderTransform>
<TranslateTransform
Name="MyAnimatedTranslateTransform4"
X="0"Y="0" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTriggerRoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="MyAnimatedTranslateTransform4"
Storyboard.TargetProperty="X"
Duration="0:0:10">
<LinearDoubleKeyFrameValue="100"KeyTime="0:0:3" />
<LinearDoubleKeyFrameValue="200"KeyTime="0:0:8" />
<LinearDoubleKeyFrameValue="500"KeyTime="0:0:9" />
<LinearDoubleKeyFrameValue="600"KeyTime="0:0:10" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>
III.页面对应的JS文件代码(Default.aspx.js)
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "1000",
height: "1000",
version: "1.0",
background: "white",
isWindowless: "true",
enableHtmlAccess: "true"
},
events: {}
});
}
|
(三). 本入门教程示例代码下载
https://files.cnblogs.com/MVP33650/Silverlight(ByChengKing).rar