微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight的cookie操作

本文让我们一起来学习如何利用Silverlight的HtmlPage.Document对象,实现在Silverlight操作Cookies.
    首先创建一个新的Silverlight应用程序,如图:
    


为使用HtmlPage.Document对象,我们要引入命名空间
  using  System.Windows.browser;  // 引入此空间以便于使用Cookie操作
为操作Cookie,我们创建一个类:SLCookieHelp.cs,此类的操作包括
 1、添加key-value键值对
 2、根据key删除
 3、根据key读取value
 4、读取cookie所有内容
 5、判断key-value是否在cookie中存在
 代码如下:

复制代码

using  System;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Ink;

using  System.Windows.Input;

using  System.Windows.Media;

using  System.Windows.Media.Animation;

using  System.Windows.Shapes;

引入此空间以便于使用Cookie操作


namespace  SLCookieOperate

{

    
public class SLCookieHelp

    
{

        
#region Cookie相关操作函数


        
#region 设置持久时间长的Cookie

        
/// <summary>

        
 设置持久时间长的Cookie

        
</summary><param name="key">the cookie key</param>

        
<param name="value">the cookie value</param>

        public  static void SetCookie(string key, string value)

        
{


            
string oldCookie = HtmlPage.Document.GetProperty("cookie"as String;


            DateTime expiration 
= DateTime.UtcNow + TimeSpan.FromDays(2000);


            
string cookie = String.Format({0}={1};expires={2}", key, value, expiration.ToString(R"));


            HtmlPage.Document.SetProperty(


        }

        
#endregion


        
#region 读取一个已经存在的Cookie

        
@H_680_404@ 读取一个已经存在的Cookie

        
cookie key<returns>null if the cookie does not exist, otherwise the cookie value</returns>string GetCookie(string key)

        
{

            
string[] cookies = HtmlPage.Document.Cookies.Split(';');

            key 
+= =';

            
foreach (string cookie in cookies)

            
{

                
string cookieStr = cookie.Trim();

                
if (cookieStr.StartsWith(key, StringComparison.OrdinalIgnoreCase))

                
{

                    
string[] vals = cookieStr.Split(');

                    
if (vals.Length >= 2)

                    
{

                        
return vals[1];

                    }

                    
return string.Empty;

                }

            }


            
null;

        }

        
#endregion


        
#region   删除特定的Cookie(清空它的Value值,过期值设置为-1天)

        
 删除特定的Cookie(清空它的Value值,过期值设置为-1天)

        
the cookie key to delete


        
void DeleteCookie(string key)

        
{

            
as String;

            DateTime expiration 
= DateTime.UtcNow - TimeSpan.FromDays(1);

            
{0}=;expires={1}"));

            HtmlPage.Document.SetProperty(

        }

        
#endregion


        
#region 判定指定的key-value对是否在cookie中存在

        
bool Exists(String key, String value)

        
{

            
return HtmlPage.Document.Cookies.Contains(

                String.Format(
{0}={1}

                );

        }

        
#endregion


        
#region  获取当前cookie内容

        
string getCookieContent()

        
{

            
return HtmlPage.Document.GetProperty(as String;

        }

        
#endregion


        
#endregion

    }

}

复制代码


创建用户界面如下图:


用户界面xaml代码如下:

复制代码

< UserControl x:Class = " SLCookieOperate.Page "
    xmlns
= http://schemas.microsoft.com/winfx/2006/xaml/presentation "  
    xmlns:x
= http://schemas.microsoft.com/winfx/2006/xaml "  
    Width
= 400 "  Height = " >
    
< Grid x:Name = LayoutRoot "  Background = White " >
        
< StackPanel >
            
< TextBlock Text = Silverlight操作Cookie学习总结 "  TextAlignment = Center "  Margin = 8 "  FontSize = 16 "  Foreground = red "   ></ TextBlock >
            
< TextBlock Text = 请输入Key 0,10,0 " ></ TextBlock >
            
< TextBox x:Name = txtBxKey "  Width = 250 " ></ TextBox >
            
< TextBlock Text = 请输入Value txtBxValue "   Width = " ></ TextBox >
            
< Button Width = 200 30 "  Content = 添加key-value对入cookie 4 "  Click = Button_Click "   ></ Button >
            
< Button Width = 根据key删除key-value对 Button_Click_1 " ></ Button >
            
< Button Width = 根据key读取对应value Button_Click_2 读取当前cookie内容 Button_Click_4 判断存在key-value对 Button_Click_3 " ></ Button >
            
< TextBlock Text = 当前的Cookie内容 Red " ></ TextBlock >
            
< TextBlock x:Name = tbCookie Green "  textwrapping = Wrap 280 "   ></ TextBlock >
            
        
</ StackPanel >
    
</ Grid >
</ UserControl >
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.Threading;
using  System.Text;
using  System.IO;


namespace  SLCookieOperate
{
    
public   partial   class  Page : UserControl
    {

        
public  Page()
        {
            InitializeComponent();

        }


        
#region   清除文本输入框
        
private   void  txtBxClear()
        {
            
this .txtBxKey.Text  =   "" ;
            
this .txtBxValue.Text  =   "" ;
        }

        
#endregion

        
void  Button_Click( object  sender, RoutedEventArgs e)
        {
            
try
            {
                
string  keyStr  =   this .txtBxKey.Text.ToString();
                
string  valueStr  =   this .txtBxValue.Text.ToString();
                
if  ( ! (String.IsNullOrEmpty(keyStr)  ||  String.IsNullOrEmpty(valueStr)))
                {
                    SLCookieHelp.SetCookie(keyStr, valueStr);

                    MessageBox.Show(
添加成功! " );
                    txtBxClear();

                    
 string kk = HtmlPage.Document.Cookies;
                     this .tbCookie.Text  =  SLCookieHelp.getCookieContent();
                }
                
else
                {
                    MessageBox.Show(
请输入key-value键值对! " );
                }


            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        
void  Button_Click_1( this .txtBxKey.Text.ToString();

            
if  ( ! String.IsNullOrEmpty(keyStr))
            {
                SLCookieHelp.DeleteCookie(keyStr);
                MessageBox.Show(
成功删除 " );
                
this .tbCookie.Text  =  SLCookieHelp.getCookieContent();

            }
            
else
            {
                MessageBox.Show(
请输入要删除的Key " );
            }
        }

        
void  Button_Click_2(         {

            
if  ( ! String.IsNullOrEmpty(keyStr))
            {
                
string  valueStr  =  SLCookieHelp.GetCookie(keyStr);

                MessageBox.Show(
string .Format( 对应你输入的Key的Value是: {0} " ,valueStr));
            }
            
请输入要查找的Key " );
            }

        }

        
void  Button_Click_3( this .txtBxKey.Text.ToString();
            
this .txtBxValue.Text.ToString();

            
if  ( ! (String.IsNullOrEmpty(keyStr)  ||  String.IsNullOrEmpty(valueStr)))
            {
                
if  (SLCookieHelp.Exists(keyStr, valueStr))
                {
                    MessageBox.Show(
此key-value对存在于cookie中 " );
                }
                
没有找到此 key-value " );
                }
               
            }
            
请输入要判定的Key与Value对 void  Button_Click_4( this .tbCookie.Text  =  SLCookieHelp.getCookieContent();
        }
    }
}

复制代码


 运行效果如下:


前往:Silverlight学习笔记清单 


原文地址:http://www.cnblogs.com/wsdj-ITtech/archive/2009/08/15/1546815.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐