[源碼下載]
作者:webabcd
介紹
Silverlight 2.0 提示和技巧系列
- Cookie - 通過 JavaScript 操作 Cookie
- 自定義字體 - 在程序中使用自定字體
- 爲程序傳遞參數 - 爲 Silverlight 程序傳遞初始化參數
- 自定義鼠標右鍵 - 響應並處理自定義的鼠標右鍵事件
- 程序常用配置參數 - object 標記的常用參數,以及對應的 Silverlight 控件的常用屬性
在線DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
示例
1、操作 Cookie 的演示
Cookie.xaml
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Margin="5">
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="cookie-key: " />
<TextBox x:Name="txtKey" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="cookie-value: " />
<TextBox x:Name="txtValue" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<Button x:Name="btnSetCookie" Content="設置Cookie" Click="btnSetCookie_Click" />
<Button x:Name="btnGetCookie" Content="獲取Cookie" Click="btnGetCookie_Click" />
<Button x:Name="btnDeleteCookie" Content="清除Cookie" Click="btnDeleteCookie_Click" />
</StackPanel>
<TextBox x:Name="txtResult" Margin="5" />
</StackPanel>
</UserControl>
Cookie.xaml.cs
關於使用 JavaScript 操作 Cookie 參看
http://msdn.microsoft.com/en-us/library/ms533693(VS.85).aspx
*/
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;
using System.Text.RegularExpressions;
namespace Silverlight20.Tip
{
public partial class Cookie : UserControl
{
public Cookie()
{
InitializeComponent();
}
/// <summary>
/// 設置 Cookie
/// </summary>
private void btnSetCookie_Click(object sender, RoutedEventArgs e)
{
if (txtKey.Text.Trim() != "" && txtValue.Text.Trim() != "")
{
string expire = DateTime.Now.AddDays(1).ToString("R"); // RFC1123Pattern 日期格式
string cookie = string.Format("{0}={1};expires={2}",
txtKey.Text.Trim(),
txtValue.Text.Trim(),
expire);
// 通過 JavaScript 設置 Cookie
// 如下語句等於在 JavaScript 中給 document.cookie 賦值
HtmlPage.Document.SetProperty("cookie", cookie);
}
}
/// <summary>
/// 獲取 Cookie
/// </summary>
private void btnGetCookie_Click(object sender, RoutedEventArgs e)
{
txtResult.Text = "";
// 通過 JavaScript 獲取 Cookie
// HtmlPage.Document.Cookies 就是 JavaScript 中的 document.cookie
string[] cookies = Regex.Split(HtmlPage.Document.Cookies, "; ");
foreach (var cookie in cookies)
{
string[] keyValue = cookie.Split('=');
if (keyValue.Length == 2)
{
txtResult.Text += keyValue[0] + " : " + keyValue[1];
txtResult.Text += "\n";
}
}
}
/// <summary>
/// 刪除 Cookie
/// </summary>
private void btnDeleteCookie_Click(object sender, RoutedEventArgs e)
{
string[] cookies = Regex.Split(HtmlPage.Document.Cookies, "; ");
foreach (var cookie in cookies)
{
string[] keyValue = cookie.Split('=');
if (keyValue.Length == 2)
{
HtmlPage.Document.SetProperty("cookie", keyValue[0] + "=;" + DateTime.Now.AddDays(-1).ToString("R"));
}
}
}
}
}
2、演示如何使用自定義字體
以使用華文行楷字體爲例,先將字體文件做爲資源型文件添加到項目裏
CustomFont.xaml
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Margin="5">
<TextBlock x:Name="lblMsg" Text="自定義字體" FontSize="50" />
<!--以聲明的方式使用自定義字體-->
<!--FontFamily - 字體源地址#字體名稱-->
<TextBlock Text="自定義字體" FontSize="50" FontFamily="/Silverlight20;component/Resource/STXINGKA.TTF#STXingkai" />
<!--
資源型文件 - [/程序集名;component/路徑]
內容型文件 - [/路徑]
-->
</StackPanel>
</UserControl>
CustomFont.xaml.cs
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.Resources;
namespace Silverlight20.Tip
{
public partial class CustomFont : UserControl
{
public CustomFont()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(CustomFont_Loaded);
}
void CustomFont_Loaded(object sender, RoutedEventArgs e)
{
// 以編碼的方式使用自定義字體
// 以華文行楷爲例
StreamResourceInfo sri = App.GetResourceStream(
new Uri("/Silverlight20;component/Resource/STXINGKA.TTF", UriKind.Relative));
// 設置需要顯示的字體源
lblMsg.FontSource = new FontSource(sri.Stream);
// 設置需要顯示的字體名稱
// STXingkai - 華文行楷的字體名稱
lblMsg.FontFamily = new FontFamily("STXingkai");
}
}
}
3、演示如何爲 Silverlight 程序傳遞初始化參數
爲 object 標記配置參數:initParams, 多個參數用“,”分隔
App.xaml.cs
{
// e.InitParams - 獲取傳遞給 Silverlight插件 的參數
foreach (var param in e.InitParams)
{
// 將參數保存到應用程序級別的資源內
Resources.Add(param.Key, param.Value);
}
this.RootVisual = new Page();
}
InitParams.xaml
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Margin="5">
<TextBlock x:Name="lblMsg" />
<!--以聲明的方式讀取應用程序級別的資源-->
<TextBlock Text="{StaticResource key2}" />
</StackPanel>
</UserControl>
InitParams.xaml.cs
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;
namespace Silverlight20.Tip
{
public partial class InitParams : UserControl
{
public InitParams()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(InitParams_Loaded);
}
void InitParams_Loaded(object sender, RoutedEventArgs e)
{
// 以編碼的方式讀取應用程序級別的資源
lblMsg.Text += App.Current.Resources["key1"];
}
}
}
4、演示如何響應並處理鼠標右鍵事件
爲 Silverlight 插件配置參數,windowless="true"
RightClick.xaml
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border BorderBrush="Black" BorderThickness="4" Background="Bisque" Width="100" HorizontalAlignment="Left">
<!--右鍵菜單的內容-->
<StackPanel>
<TextBlock Margin="5">我是右鍵菜單1</TextBlock>
<TextBlock Margin="5">我是右鍵菜單2</TextBlock>
<TextBlock Margin="5">我是右鍵菜單3</TextBlock>
</StackPanel>
<!--右鍵菜單的位置-->
<Border.RenderTransform>
<TranslateTransform x:Name="tt" />
</Border.RenderTransform>
</Border>
</UserControl>
RightClick.xaml.cs
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 RightClick : UserControl
{
public RightClick()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(RightClick_Loaded);
}
void RightClick_Loaded(object sender, RoutedEventArgs e)
{
// 監聽頁面的 oncontextmenu 事件,並處理
// 注:如果要監聽 oncontextmenu 事件,需要將 Silverlight 程序的 Windowless 屬性設置爲 true
HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);
}
private void OnContextMenu(object sender, HtmlEventArgs e)
{
// 設置右鍵菜單出現的位置
tt.X = e.OffsetX - 201;
tt.Y = e.OffsetY - 30;
// 禁止其他 DOM 響應該事件(屏蔽掉默認的右鍵菜單)
// 相當於 event.returnValue = false;
e.PreventDefault();
}
}
}
5、Silverlight 程序的常用配置參數的說明
ParamDemo.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Silverlight20</title>
<style type="text/css">
html, body
{
height: 100%;
overflow: auto;
}
body
{
padding: 0;
margin: 0;
}
#silverlightControlHost
{
height: 100%;
}
</style>
<script type="text/javascript" src="../Silverlight.js"></script>
</head>
<body>
<div id="silverlightControlHost">
<!--
注:括號裏爲 Silverlight 控件所對應的屬性
source(Source) - xap 文件的路徑
minRuntimeVersion(MinimumVersion) - 所需的最低 Silverlight 插件版本
autoUpgrade(AutoUpgrade) - Silverlight 插件是否要自動升級。默認值 true
initParams(InitParameters) - 爲 Silverlight 程序傳遞初始化參數。用“,”分隔
enableFrameRateCounter(EnableFrameRateCounter) - 是否在宿主瀏覽器的狀態欄中顯示當前呈現的 Silverlight 內容的每秒幀數(fps),用於調試用。默認值 false
maxFrameRate(MaxFrameRate) - 每秒要呈現的最大幀數。默認值 0 (表示未指定最大幀數)
enableRedrawRegions(EnableRedrawRegions) - 是否顯示每個幀所重繪的區域。默認值 false
enableHtmlAccess(HtmlAccess) - 是否允許 HTML DOM 訪問
對於 object 標記的 param : value="true" - 允許;value="false" - 不允許;無此 param - 同域允許
對於 Silverlight 控件的 HtmlAccess 屬性 : Enabled - 允許;Disabled - 不允許;SameDomain - 同域允許
windowless(Windowless) - 指定 Silverlight 插件是否爲無窗口插件
-->
<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" />
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="key1=value1,key2=value2" />
<param name="enableFrameRateCounter" value="true" />
<param name="maxFrameRate" value="30" />
<param name="enableRedrawRegions" value="false" />
<param name="enableHtmlAccess" value="true" />
<param name="windowless" value="true" />
</object>
<iframe style='visibility: hidden; height: 0; width: 0; border: 0px'></iframe>
<!--iframe 元素和其他附加到 HTML 的元素有助於確保跨瀏覽器兼容性。iframe 的存在可避免 Safari 瀏覽器緩存頁面。當用戶向後導航到以前訪問過的 Silverlight 頁面時,Safari 緩存可避免重新加載 Silverlight 插件-->
</div>
</body>
</html>
OK
[源碼下載]