稳扎稳打Silverlight(29) - 2.0TipTrick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键

稳扎稳打Silverlight(29) - 2.0TipTrick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键,第1张

概述[索引页] [源码下载] 稳扎稳打Silverlight(29) - 2.0Tip/Trick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键, 程序常用配置参数 作者:webabcd 介绍 Silverlight 2.0 提示和技巧系列 Cookie - 通过 JavaScript  *** 作 Cookie 自定义字体 - 在程序中使用自定字体 为程序传递参数 - 为 Silverli [索引页]
[源码下载]


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

作者:webabcd


介绍
Silverlight 2.0 提示和技巧系列
cookie - 通过 JavaScript  *** 作 cookie 自定义字体 - 在程序中使用自定字体 为程序传递参数 - 为 Silverlight 程序传递初始化参数 自定义鼠标右键 - 响应并处理自定义的鼠标右键事件 程序常用配置参数 - object 标记的常用参数,以及对应的 Silverlight 控件的常用属性 

在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.html 


示例
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

/*

关于使用 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() != @H_301_268@"" && txtValue.Text.Trim() != "")

                        {

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

                                 string cookie = string.Format( @H_301_268@"{0}={1};expires={2}",

                                        txtKey.Text.Trim(),

                                        txtValue.Text.Trim(),

                                        expire);


                                 // 通过 JavaScript 设置 cookie

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

                                HTMLPage.document.SetProperty( @H_301_268@"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,@H_301_268@"; ");


                         foreach (var cookie in cookies)

                        {

                                 string[] keyvalue = cookie.Split('=');


                                 if (keyvalue.Length == 2)

                                {

                                        txtResult.Text += keyvalue[0] + @H_301_268@" : " + keyvalue[1];

                                        txtResult.Text += @H_301_268@"\n";

                                }

                        }

                }


                 /// <summary>

                 /// 删除 cookie

                 /// </summary>

                 private voID btnDeletecookie_Click( object sender,RoutedEventArgs e)

                {

                         string[] cookies = Regex.Split(HTMLPage.document.cookies,@H_301_268@"; ");


                         foreach (var cookie in cookies)

                        {

                                 string[] keyvalue = cookie.Split('=');


                                 if (keyvalue.Length == 2)

                                {

                                        HTMLPage.document.SetProperty( @H_301_268@"cookie",keyvalue[0] + @H_301_268@"=;" + DateTime.Now.AddDays(-1).ToString( @H_301_268@"R"));

                                }

                        }

                }

        }

}    
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

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.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( @H_301_268@"/Silverlight20;component/Resource/STXINGKA.TTF",UriKind.relative));


                         // 设置需要显示的字体源

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


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

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

                        lblMsg.FontFamily = new FontFamily( @H_301_268@"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"
        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;

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[ @H_301_268@"key1"];

                }

        }

}
  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

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;


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( @H_301_268@"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
< @H_301_268@!DOCTYPE HTML PUBliC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd" >
< @H_301_268@HTML xmlns ="http://www.w3.org/1999/xhtml" >
< @H_301_268@head >
         < @H_301_268@Title >Silverlight20 </@H_301_268@@H_301_268@Title>
         < @H_301_268@style type ="text/CSS" >
                HTML,body
                {
                        height: 100%;
                        overflow: auto;
                }
                body
                {
                        padding: 0;
                        margin: 0;
                }
                #silverlightControlHost
                {
                        height: 100%;
                }
         </@H_301_268@@H_301_268@style>

         < @H_301_268@script type ="text/JavaScript" src ="../Silverlight.Js" > </@H_301_268@@H_301_268@script>

</@H_301_268@@H_301_268@head>
< @H_301_268@body >
         < @H_301_268@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%">
                         < @H_301_268@param name ="source" value ="../ClIEntBin/Silverlight20.xap" />
                         < @H_301_268@param name ="minRuntimeVersion" value ="2.0.31005.0" />
                         < @H_301_268@param name ="autoUpgrade" value ="true" />
                         < @H_301_268@param name ="initParams" value ="key1=value1,key2=value2" />
                         < @H_301_268@param name ="enableFrameRateCounter" value ="true" />
                         < @H_301_268@param name ="maxFrameRate" value ="30" />
                         < @H_301_268@param name ="enableRedrawRegions" value ="false" />
                         < @H_301_268@param name ="enableHTMLAccess" value ="true" />
                         < @H_301_268@param name ="windowless" value ="true" />
                 </@H_301_268@@H_301_268@object>
                 < @H_301_268@iframe style ='visibility: hIDden; height: 0; wIDth: 0; border: 0px' > </@H_301_268@@H_301_268@iframe>
                 < @H_301_268@! --iframe 元素和其他附加到 HTML 的元素有助于确保跨浏览器兼容性。iframe 的存在可避免 Safari 浏览器缓存页面。当用户向后导航到以前访问过的 Silverlight 页面时,Safari 缓存可避免重新加载 Silverlight 插件-- >
         </@H_301_268@@H_301_268@div>
</@H_301_268@@H_301_268@body>
</@H_301_268@@H_301_268@HTML>     OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(29) - 2.0Tip/Trick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键全部内容,希望文章能够帮你解决稳扎稳打Silverlight(29) - 2.0Tip/Trick之Cookie, 自定义字体, 为程序传递参数, 自定义鼠标右键所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1051586.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存