Silverlight读取Cookie指导手册介绍

Silverlight开发工具为开发人员带来了很大的好处。在多媒体的处理上,帮助开发人员摆脱以前的种种约束轻松实现各种以前只能依靠美工才能实现的功能。在这里我们先来了解一下Silverlight读取Cookie的相关操作方法。#t#

我们要想实现Silverlight读取Cookie的话,可以通过HtmlPage.Document.GetProperty方法来获取所有Cookie,另外在HtmlDocument中定义了Cookies属性,已经为我们封装好了GetProperty方法,可以直接使用,它的定义如下代码所示:

  1. public sealed class 
    HtmlDocument : HtmlObject  
  2. {  
  3. public string Cookies  
  4. {  
  5. get{  
  6. HtmlPage.VerifyThread();  
  7. String property = this.
    GetProperty("cookie") as String;  
  8. if (property != null)  
  9. {  
  10. return property;  
  11. }  
  12. return String.Empty;  
  13. }  
  14. set{  
  15. HtmlPage.VerifyThread();  
  16. String str = value;  
  17. if (String.IsNullOrEmpty(str))  
  18. {  
  19. str = string.Empty;  
  20. }  
  21. this.SetProperty("cookie", str);  
  22. }  
  23. }  

如使用下面这段Silverlight读取Cookie代码来获取一个指定Key的Cookie值:

 

 
 
 
  1. void btnRetrieve_Click(object
     sender, RoutedEventArgs e)  
  2. {  
  3. String[] cookies = HtmlPage.
    Document.Cookies.Split(';');  
  4. foreach (String cookie in cookies)  
  5. {  
  6. String[] keyValues = cookie.Split('=');  
  7. if (keyValues.Length == 2)  
  8. {  
  9. if (keyValues[0].Trim() == 
    this.txtKey.Text.Trim())  
  10. {  
  11. this.txtValue.Text = keyValues[1];  
  12. }  
  13. }  
  14. }  

Silverlight读取Cookie的具体方法就为大家介绍到这里。

THE END