<a href="http://webabcd.blog.51cto.com/1787395/342790" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>
穩紮穩打Silverlight(29) - 2.0Tip/Trick之Cookie, 自定義字型, 為程式傳遞參數, 自定義滑鼠右鍵, 程式常用配置參數
介紹
Silverlight 2.0 提示和技巧系列
Cookie - 通過 JavaScript 操作 Cookie
自定義字型 - 在程式中使用自定字型
為程式傳遞參數 - 為 Silverlight 程式傳遞初始化參數
自定義滑鼠右鍵 - 響應并處理自定義的滑鼠右鍵事件
程式常用配置參數 - object 标記的常用參數,以及對應的 Silverlight 控件的常用屬性
線上DEMO
示例
1、操作 Cookie 的示範
Cookie.xaml
<UserControl x:Class="Silverlight20.Tip.Cookie"
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>
<TextBlock Text="cookie-value: " />
<TextBox x:Name="txtValue" />
<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" />
<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);
}
/// 擷取 Cookie
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";
}
/// 删除 Cookie
private void btnDeleteCookie_Click(object sender, RoutedEventArgs e)
HtmlPage.Document.SetProperty("cookie", keyValue[0] + "=;" + DateTime.Now.AddDays(-1).ToString("R"));
}
}
2、示範如何使用自定義字型
以使用華文行楷字型為例,先将字型檔案做為資源型檔案添加到項目裡
CustomFont.xaml
<UserControl x:Class="Silverlight20.Tip.CustomFont"
<TextBlock x:Name="lblMsg" Text="自定義字型" FontSize="50" />
<!--以聲明的方式使用自定義字型-->
<!--FontFamily - 字型源位址#字型名稱-->
<TextBlock Text="自定義字型" FontSize="50" FontFamily="/Silverlight20;component/Resource/STXINGKA.TTF#STXingkai" />
<!--
資源型檔案 - [/程式集名;component/路徑]
内容型檔案 - [/路徑]
-->
CustomFont.xaml.cs
using System.Windows.Resources;
public partial class CustomFont : UserControl
public CustomFont()
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, 多個參數用“,”分隔
<param name="initParams" value="key1=value1,key2=value2" />
或者為 Silverlight 控件配置屬性:InitParameters, 多個參數用“,”分隔
<asp:Silverlight ID="Xaml1" runat="server" InitParameters="key1=value1,key2=value2" />
App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
// e.InitParams - 擷取傳遞給 Silverlight插件 的參數
foreach (var param in e.InitParams)
// 将參數儲存到應用程式級别的資源内
Resources.Add(param.Key, param.Value);
this.RootVisual = new Page();
InitParams.xaml
<UserControl x:Class="Silverlight20.Tip.InitParams"
<TextBlock x:Name="lblMsg" />
<!--以聲明的方式讀取應用程式級别的資源-->
<TextBlock Text="{StaticResource key2}" />
InitParams.xaml.cs
public partial class InitParams : UserControl
public InitParams()
this.Loaded += new RoutedEventHandler(InitParams_Loaded);
void InitParams_Loaded(object sender, RoutedEventArgs e)
// 以編碼的方式讀取應用程式級别的資源
lblMsg.Text += App.Current.Resources["key1"];
}
4、示範如何響應并處理滑鼠右鍵事件
為 Silverlight 插件配置參數,windowless="true"
<param name="windowless" value="true" />
RightClick.xaml
<UserControl x:Class="Silverlight20.Tip.RightClick"
<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>
<!--右鍵菜單的位置-->
<Border.RenderTransform>
<TranslateTransform x:Name="tt" />
</Border.RenderTransform>
</Border>
RightClick.xaml.cs
public partial class RightClick : UserControl
public RightClick()
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
<!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>Silverlight20</title>
<style type="text/css">
html, body
height: 100%;
overflow: auto;
body
padding: 0;
margin: 0;
#silverlightControlHost
</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
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343931,如需轉載請自行聯系原作者