Cookie is a frequently used storage method in browsers. It is easy to read and maintain. May someone want to use cookies in Silverlight? The answer is inevitable! Next, let me introduce how to use cookies in SL ~
Creating a cookie also uses our old friend htmlpage.doc ument;
/**/ /// <Summary>
/// Create cookie
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Private Void Setcookie ( String Key, String Value)
{
Datetime expiredate = Datetime. Now + Timespan. fromdays ( 7 ); // Valid for one week
String Newcookie = Key + " = " + Value + " ; Expires = " + Expiredate. tostring ( " R " );
Htmlpage. Document. setproperty ( " Cookie " , Newcookie );
}
Reading cookies is also simple.
/**/ /// <Summary>
/// Read cookie
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Private String Getcookie ( String Key)
{
String [] Cookies = Htmlpage. Document. Cookies. Split ( ' ; ' );
Foreach ( String Cookie In Cookies)
{
String [] KeyValue = Cookie. Split ( ' = ' );
If (KeyValue. Length = 2 )
{
If (KeyValue [ 0 ]. Tostring () = Key)
{
ReturnKeyValue [1];
}
}
} Return Null ;
}
Source code:Set browser cookie