[索引页]

[源码下载]




稳扎稳打Silverlight(29) - 2.0Tip/Trick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键, 程序常用配置参数





作者:webabcd





介绍

Silverlight 2.0 提示和技巧系列

  • Cookie - 通过 JavaScript 操作 Cookie
  • 自定义字体 - 在程序中使用自定字体
  • 为程序传递参数 - 为 Silverlight 程序传递初始化参数
  • 自定义鼠标右键 - 响应并处理自定义的鼠标右键事件
  • 程序常用配置参数 - object 标记的常用参数,以及对应的 Silverlight 控件的常用属性





在线DEMO

http://webabcd.blog.51cto.com/1787395/342779 





示例

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> 



                <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
InBlock.gif/* 
InBlock.gif关于使用 JavaScript 操作 Cookie 参看 
InBlock.gifhttp://msdn.microsoft.com/en-us/library/ms533693(VS.85).aspx    
InBlock.gif*/ 

InBlock.gifusing System; 

InBlock.gifusing System.Collections.Generic; 

InBlock.gifusing System.Linq; 

InBlock.gifusing System.Net; 

InBlock.gifusing System.Windows; 

InBlock.gifusing System.Windows.Controls; 

InBlock.gifusing System.Windows.Documents; 

InBlock.gifusing System.Windows.Input; 

InBlock.gifusing System.Windows.Media; 

InBlock.gifusing System.Windows.Media.Animation; 

InBlock.gifusing System.Windows.Shapes; 

InBlock.gif 

InBlock.gifusing System.Windows.Browser; 

InBlock.gifusing System.Text.RegularExpressions; 

InBlock.gif 

InBlock.gifnamespace Silverlight20.Tip 

InBlock.gif

InBlock.gif        public partial class Cookie : UserControl 

InBlock.gif        { 

InBlock.gif                public Cookie() 

InBlock.gif                { 

InBlock.gif                        InitializeComponent(); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                /// <summary> 

InBlock.gif                /// 设置 Cookie 

InBlock.gif                /// </summary> 

InBlock.gif                private void btnSetCookie_Click(object sender, RoutedEventArgs e) 

InBlock.gif                { 

InBlock.gif                        if (txtKey.Text.Trim() != "" && txtValue.Text.Trim() != "") 

InBlock.gif                        { 

InBlock.gif                                string expire = DateTime.Now.AddDays(1).ToString("R"); // RFC1123Pattern 日期格式 

InBlock.gif                                string cookie = string.Format("{0}={1};expires={2}", 

InBlock.gif                                        txtKey.Text.Trim(), 

InBlock.gif                                        txtValue.Text.Trim(), 

InBlock.gif                                        expire); 

InBlock.gif 

InBlock.gif                                // 通过 JavaScript 设置 Cookie 

InBlock.gif                                // 如下语句等于在 JavaScript 中给 document.cookie 赋值 

InBlock.gif                                HtmlPage.Document.SetProperty("cookie", cookie); 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                /// <summary> 

InBlock.gif                /// 获取 Cookie 

InBlock.gif                /// </summary> 

InBlock.gif                private void btnGetCookie_Click(object sender, RoutedEventArgs e) 

InBlock.gif                { 

InBlock.gif                        txtResult.Text = ""; 

InBlock.gif 

InBlock.gif                        // 通过 JavaScript 获取 Cookie 

InBlock.gif                        // HtmlPage.Document.Cookies 就是 JavaScript 中的 document.cookie 

InBlock.gif                        string[] cookies = Regex.Split(HtmlPage.Document.Cookies, "; "); 

InBlock.gif 

InBlock.gif                        foreach (var cookie in cookies) 

InBlock.gif                        { 

InBlock.gif                                string[] keyValue = cookie.Split('='); 

InBlock.gif 

InBlock.gif                                if (keyValue.Length == 2) 

InBlock.gif                                { 

InBlock.gif                                        txtResult.Text += keyValue[0] + " : " + keyValue[1]; 

InBlock.gif                                        txtResult.Text += "\n"; 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                /// <summary> 

InBlock.gif                /// 删除 Cookie 

InBlock.gif                /// </summary> 

InBlock.gif                private void btnDeleteCookie_Click(object sender, RoutedEventArgs e) 

InBlock.gif                { 

InBlock.gif                        string[] cookies = Regex.Split(HtmlPage.Document.Cookies, "; "); 

InBlock.gif 

InBlock.gif                        foreach (var cookie in cookies) 

InBlock.gif                        { 

InBlock.gif                                string[] keyValue = cookie.Split('='); 

InBlock.gif 

InBlock.gif                                if (keyValue.Length == 2) 

InBlock.gif                                { 

InBlock.gif                                        HtmlPage.Document.SetProperty("cookie", keyValue[0] + "=;" + DateTime.Now.AddDays(-1).ToString("R")); 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}

2、演示如何使用自定义字体
以使用华文行楷字体为例,先将字体文件做为资源型文件添加到项目里
CustomFont.xaml
<UserControl x:Class="Silverlight20.Tip.CustomFont" 

        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
InBlock.gifusing System; 

InBlock.gifusing System.Collections.Generic; 

InBlock.gifusing System.Linq; 

InBlock.gifusing System.Net; 

InBlock.gifusing System.Windows; 

InBlock.gifusing System.Windows.Controls; 

InBlock.gifusing System.Windows.Documents; 

InBlock.gifusing System.Windows.Input; 

InBlock.gifusing System.Windows.Media; 

InBlock.gifusing System.Windows.Media.Animation; 

InBlock.gifusing System.Windows.Shapes; 

InBlock.gif 

InBlock.gifusing System.Windows.Resources; 

InBlock.gif 

InBlock.gifnamespace Silverlight20.Tip 

InBlock.gif

InBlock.gif        public partial class CustomFont : UserControl 

InBlock.gif        { 

InBlock.gif                public CustomFont() 

InBlock.gif                { 

InBlock.gif                        InitializeComponent(); 

InBlock.gif 

InBlock.gif                        this.Loaded += new RoutedEventHandler(CustomFont_Loaded); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                void CustomFont_Loaded(object sender, RoutedEventArgs e) 

InBlock.gif                { 

InBlock.gif                        // 以编码的方式使用自定义字体 

InBlock.gif 

InBlock.gif                        // 以华文行楷为例 

InBlock.gif                        StreamResourceInfo sri = App.GetResourceStream( 

InBlock.gif                            new Uri("/Silverlight20;component/Resource/STXINGKA.TTF", UriKind.Relative)); 

InBlock.gif 

InBlock.gif                        // 设置需要显示的字体源 

InBlock.gif                        lblMsg.FontSource = new FontSource(sri.Stream); 

InBlock.gif 

InBlock.gif                        // 设置需要显示的字体名称 

InBlock.gif                        // STXingkai - 华文行楷的字体名称 

InBlock.gif                        lblMsg.FontFamily = new FontFamily("STXingkai"); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
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
InBlock.gifprivate void Application_Startup(object sender, StartupEventArgs e) 

InBlock.gif

InBlock.gif        // e.InitParams - 获取传递给 Silverlight插件 的参数 

InBlock.gif 

InBlock.gif        foreach (var param in e.InitParams) 

InBlock.gif        { 

InBlock.gif                // 将参数保存到应用程序级别的资源内 

InBlock.gif                Resources.Add(param.Key, param.Value); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        this.RootVisual = new Page(); 

InBlock.gif}
InitParams.xaml
<UserControl x:Class="Silverlight20.Tip.InitParams" 

        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
InBlock.gifusing System; 

InBlock.gifusing System.Collections.Generic; 

InBlock.gifusing System.Linq; 

InBlock.gifusing System.Net; 

InBlock.gifusing System.Windows; 

InBlock.gifusing System.Windows.Controls; 

InBlock.gifusing System.Windows.Documents; 

InBlock.gifusing System.Windows.Input; 

InBlock.gifusing System.Windows.Media; 

InBlock.gifusing System.Windows.Media.Animation; 

InBlock.gifusing System.Windows.Shapes; 

InBlock.gif 

InBlock.gifnamespace Silverlight20.Tip 

InBlock.gif

InBlock.gif        public partial class InitParams : UserControl 

InBlock.gif        { 

InBlock.gif                public InitParams() 

InBlock.gif                { 

InBlock.gif                        InitializeComponent(); 

InBlock.gif 

InBlock.gif                        this.Loaded += new RoutedEventHandler(InitParams_Loaded); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                void InitParams_Loaded(object sender, RoutedEventArgs e) 

InBlock.gif                { 

InBlock.gif                        // 以编码的方式读取应用程序级别的资源 

InBlock.gif                        lblMsg.Text += App.Current.Resources["key1"]; 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif
4、演示如何响应并处理鼠标右键事件

为 Silverlight 插件配置参数,windowless="true"
<param name="windowless" value="true" />
RightClick.xaml
<UserControl x:Class="Silverlight20.Tip.RightClick" 

        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
InBlock.gifusing System; 

InBlock.gifusing System.Collections.Generic; 

InBlock.gifusing System.Linq; 

InBlock.gifusing System.Net; 

InBlock.gifusing System.Windows; 

InBlock.gifusing System.Windows.Controls; 

InBlock.gifusing System.Windows.Documents; 

InBlock.gifusing System.Windows.Input; 

InBlock.gifusing System.Windows.Media; 

InBlock.gifusing System.Windows.Media.Animation; 

InBlock.gifusing System.Windows.Shapes; 

InBlock.gif 

InBlock.gifusing System.Windows.Browser; 

InBlock.gif 

InBlock.gifnamespace Silverlight20.Tip 

InBlock.gif

InBlock.gif        public partial class RightClick : UserControl 

InBlock.gif        { 

InBlock.gif                public RightClick() 

InBlock.gif                { 

InBlock.gif                        InitializeComponent(); 

InBlock.gif 

InBlock.gif                        this.Loaded += new RoutedEventHandler(RightClick_Loaded); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                void RightClick_Loaded(object sender, RoutedEventArgs e) 

InBlock.gif                { 

InBlock.gif                        // 监听页面的 oncontextmenu 事件,并处理 

InBlock.gif                        // 注:如果要监听 oncontextmenu 事件,需要将 Silverlight 程序的 Windowless 属性设置为 true 

InBlock.gif                        HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                private void OnContextMenu(object sender, HtmlEventArgs e)    

InBlock.gif                { 

InBlock.gif                        // 设置右键菜单出现的位置 

InBlock.gif                        tt.X = e.OffsetX - 201; 

InBlock.gif                        tt.Y = e.OffsetY - 30; 

InBlock.gif 

InBlock.gif                        // 禁止其他 DOM 响应该事件(屏蔽掉默认的右键菜单) 

InBlock.gif                        // 相当于 event.returnValue = false; 

InBlock.gif                        e.PreventDefault(); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
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 

                { 

                        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

[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/343931,如需转载请自行联系原作者

稳扎稳打Silverlight(29) - 2.0Tip/Trick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键...相关推荐

  1. 稳扎稳打Silverlight(30) - 2.0Tip/Trick之Silverlight.js, Silverlight.supportedUserAgent.js

    [索引页] [源码下载] 稳扎稳打Silverlight(30) - 2.0Tip/Trick之Silverlight.js, Silverlight.supportedUserAgent.js, 自 ...

  2. 前后台分离使用cookie判断用户状态以及传递参数

    前言 在之前学习servlet的时候,当时做的小网站需要登陆并且判断信息,当时使用session传值,使用fitter过滤判断,当时感觉哇,session咋这么好用,cookie是啥玩意,还不方便. ...

  3. 稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件

    [索引页] [×××] 稳扎稳打Silverlight(13) - 2.0交互之鼠标事件和键盘事件 作者:webabcd 介绍 Silverlight 2.0 人机交互:响应用户的鼠标操作和键盘操作 ...

  4. 稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 绑定数据到ListBox

    [索引页] [源码下载] 稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 详解ListBox 作者:webabcd 介绍 Silverlight 2.0 详解DataG ...

  5. 稳扎稳打Silverlight(8) - 2.0图形之基类System.Windows.Shapes.Shape

    [索引页] [×××] 稳扎稳打Silverlight(8) - 2.0图形之基类System.Windows.Shapes.Shape 作者:webabcd 介绍 Silverlight 2.0 图 ...

  6. 稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager

    [索引页] [源码下载] 稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager 作者:webabcd 介绍 Silverlight 3.0 控件 ...

  7. 稳扎稳打Silverlight(25) - 2.0线程之Thread, Timer, BackgroundWorker, ThreadPool

    [索引页] [源码下载] 稳扎稳打Silverlight(25) - 2.0线程之Thread, Timer, BackgroundWorker, ThreadPool 作者:webabcd 介绍 S ...

  8. 稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器...

    [索引页] [×××] 稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器 作者:webabcd 介绍 Silverlight 2.0 ...

  9. Silverlight 4.0添加鼠标右键菜单和Silverlight全屏模式的进入退出

    说明:本文出处:http://www.cnblogs.com/chengxingliang/archive/2011/02/14/1954399.html#2627673(非常感谢) 在实际应用中,我 ...

最新文章

  1. OKR落地,实践经验总结两个点比较重要
  2. 201109080909
  3. The Singleton of Design Pattern单态模式
  4. 2021-03-07 Nussbaum函数
  5. java 使用jasper_使用Jasper Reports以Java创建报告
  6. 请移步到我的新浪博客
  7. Silverlight专题(15) - 你自己的视频播放器之自定义MoveToPointSlider
  8. python列表元素都加倍_关于python列表增加元素的三种操作方法
  9. 2020前端面试(一面面试题)
  10. Linux Mint外接显示器分辨率调节
  11. java 转换为maven_Java工程转换为Maven工程
  12. spring 整合struts
  13. android token机制_Android之window机制token验证
  14. 尽点力,虽然也不一定有用
  15. 关于VisualStudio性能分析数据中的独占样本数和非独占样本数的意义
  16. Atitit 常见软件设计图纸总结 目录 1.1. ui原型图与html 2 1.2. 业务逻辑 伪代码 各种uml图 2 1.3. 总体设计图纸 结构图 层次图 架构图 2 1.4. 业务逻辑
  17. android11系统原生铃声,原生系统的凤毛麟角 索尼Xperia 1 II推送Android 11体验
  18. 初学者怎么记‘A‘,‘a‘,空格的ascii码?
  19. 这些优秀的 Spring Cloud 开源软件,你知道的有几个?
  20. 判断计算机硬件和网络故障,计算机硬件故障的识别与处理

热门文章

  1. SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理
  2. Exchange 2010 EMC 删除 DisconnectedMailbox
  3. java8学习:新的日期使用
  4. 在C#中,Json的序列化和反序列化的几种方式总结
  5. nginx 日志切割
  6. PHP中的替代语法(冒号、endif、endwhile、endfor)
  7. 快速清理Android中无用的资源信息,图片,字符串等
  8. Last_Error: Relay log read failure: Could not parse relay log event entry.
  9. 设计模式系列-建造者模式
  10. 一个在菜场看到的,神一般的大爷!