WEB开发网
开发学院图形图像Flash 稳扎稳打Silverlight(29) - 2.0Tip/Trick之Cookie... 阅读

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

 2009-05-21 12:08:20 来源:WEB开发网   
核心提示: Cookie.xaml.cs/**//*关于使用 JavaScript 操作 Cookie 参看http://msdn.microsoft.com/en-us/library/ms533693(VS.85).aspx */using System;using System.Collecti

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"));
                }
            }
        }
    }
}

上一页  1 2 3 4 5 6 7  下一页

Tags:稳扎稳打 Silverlight Tip

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接