`
terryfeng
  • 浏览: 492179 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Silverlight 与HTML元素交互操作

阅读更多

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("请输入密码");

 

 

示例出自Silverlight 3.0 开发详解与最佳实践一书

分享到:
评论

相关推荐

    如何在Silverlight控件中与Html元素交互

    如何在Silverlight控件中与Html元素交互

    Silverlight2.0功能展示Demo源码

    Silverlight 2.0 使用c#开发可脚本化的代码,Silverlight与宿主页面的DOM之间的交互,Silverlight与宿主页面的JavaScript之间的交互。 ScriptableMemberAttribute - 需要脚本化的属性、方法、事件要标记为此 ...

    Silverlight常例整理打包

    来源:...打包项目: silverlight中开发User Control light与javascript交互操作 silvelight中操作html页面元素 Silverlight中使用TabControl createObject(createObjectEx)创建silverlight对象

    使用 HTML5 canvas 进行 Web 绘图

    HTML5 canvas 元素的出现填补了这种不足,开发人员可以使用 JavaScript 脚本语言在 canvas 中进行一系列基于命令的图形绘制操作,本文将通过讲解如何使用 canvas 元素进行基本绘图操作,以及完成简单的动画和用户...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    Activity Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务,是一个负责与用户交互的组件 SSH 为 Struts+Spring+Hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架。...

    Apress.Pro.WPF.in.C.Sharp.2008.2nd.Edition.Feb.2008

    19.2.6 通过代码与元素进行交互 543 19.2.7 调整文本 547 19.3 只读流文档包容器 548 19.3.1 缩放 549 19.3.2 页面和列 549 19.3.3 从文件加载文档 552 19.3.4 打印 552 19.4 编辑流文档 553 19.4.1 加载文件 553 ...

    ASP.NET4高级程序设计第4版 带目录PDF 分卷压缩包 part1

    1.2.5 Silverlight 1.3 总结 第2章 Visual Studio 2.1 Visual Studio 2.1.1 网站和Web项目 2.1.2 创建无项目文件的网站 2.1.3 设计网页 2.2 Visual StudioIDE 2.2.1 解决方案资源管理器 2.2.2 ...

    ASP.NET4高级程序设计(第4版) 3/3

    1.2.5 Silverlight 14 1.3 总结 15 第2章 Visual Studio 16 2.1 Visual Studio 16 2.1.1 网站和Web项目 17 2.1.2 创建无项目文件的网站 18 2.1.3 设计网页 21 2.2 Visual StudioIDE 26 2.2.1 解决...

Global site tag (gtag.js) - Google Analytics