Silverlight与HTML元素交互操作以及Cookie
Silverlight 中的HtmlPage 和 HtmlElement 分别代表HTML页面和HTML元素
获取HTML元素改变他的属性
    <div>
        <img id="img1" src="Images/logo.jpg" />
    </div>xaml.cs:
private void Button1_Click(object sender, RoutedEventArgs e) 
        { 
            //获取HTML中的img1对象 
            HtmlElement img = HtmlPage.Document.GetElementById("img1"); 
            //根据输入值设置img1的html属性Width和Height 
            img.SetAttribute("width", textBox1.Text); 
            img.SetAttribute("height", textBox2.Text); 
        }
HTML元素操作Silverlight
<div><select id="sel">
            <option value="Red">红ì色?</option>
            <option value="Green">绿ì色?</option>
            <option value="Blue">蓝?色?</option>
        </select>
    </div>
xaml.cs:public htmldom2() 
        { 
            InitializeComponent(); 
            //获取select对象 
            HtmlElement select = HtmlPage.Document.GetElementById("sel"); 
            //添加select的onchange事件 
            select.AttachEvent("onchange" 
                , new EventHandler<HtmlEventArgs>(select_onChange)); 
        }
        public void select_onChange(object sender, HtmlEventArgs e) 
        { 
            //获取select的选择值 
            HtmlElement select = sender as HtmlElement; 
            string value = select.GetAttribute("value"); 
            textBlock1.Text = value; 
            //根据用户选择值来改变椭圆形的Fill值 
            switch (value) 
            { 
                case "Red": 
                    ellipse1.Fill = new SolidColorBrush(Colors.Red); 
                    break; 
                case "Green": 
                    ellipse1.Fill = new SolidColorBrush(Colors.Green); 
                    break; 
                case "Blue": 
                    ellipse1.Fill = new SolidColorBrush(Colors.Blue); 
                    break; 
            } 
        }
HTML 文本 和 URL文本的编解码
Html编码
HttpUtility.HtmlEncode(textBlock1.Text);HTML解码HttpUtility.HtmlDecode(textBlock2.Text);URL编码HttpUtility.UrlEncode(textBlock3.Text);URL解码HttpUtility.UrlDecode(textBlock4.Text);
读写Cookie
//客户端Cookie读写类 
public class CookieHelper 
{ 
    //根据Key和Value写客户端Cookie 
    public static void SetCookie(string key, string value) 
    { 
        DateTime expire = DateTime.UtcNow 
            + TimeSpan.FromDays(30); 
        string cookie = string.Format("{0}={1};expires={2}" 
            , key, value, expire.ToString("R")); 
        HtmlPage.Document.SetProperty("cookie" 
            , cookie); 
    } 
    //根据Key读客户端Cookie 
    public static string GetCookie(string key) 
    { 
        key += '='; 
        //取出所有Cookie 
        string[] cookies = 
            HtmlPage.Document.Cookies.Split(';'); 
        //遍历Cookie值 
        foreach (string cookie in cookies) 
        { 
            string cookieStr = cookie.Trim(); 
            //获取Cookie的key名称的位置 
            if (cookieStr.StartsWith(key, 
                StringComparison.OrdinalIgnoreCase)) 
            { 
                //分隔出key的值 
                string[] vals = cookieStr.Split('=');
                if (vals.Length >= 2) 
                { 
                    //返回值 
                    return vals[1]; 
                } 
                //如果没有找到则返回空白字符串 
                return string.Empty; 
            } 
        } 
        //如果没有Cookie则返回空白字符串 
        return string.Empty; 
    } 
}
在Silverlight 中使用 Dom 的Window 对象
导航
         //根据输入值创建URI对象 
         Uri uri = new Uri(tbUrl.Text, UriKind.RelativeOrAbsolute); 
         //导航到URI地址 
         HtmlPage.Window.Navigate(uri);
浏览器提示窗口
提示窗口
HtmlPage.Window.Alert("这是使用HtmlPage调用的消息框!");
判定窗口
if (HtmlPage.Window.Confirm("你确定吗?")){}
输入窗口
string password = HtmlPage.Window.Prompt("请输入密码");
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号