Silverlight 浏览器交互

通过System.Windows.Browser命名空间下的HtmlPage,HtmlDocument,HtmlElement,HtmlWindow操作浏览器对象 

(1)  HtmlDocument的属性简介

        Body:Html的Body对象

        Cookies:Cookie字符串

        DocumentElement:

        DocumentUri:Silverlight宿主的html地址

       QueryString:页面的查询字符串参数

(2)   HtmlPage的属性简介

        可以使用HtmlPage对象取得对应的HtmlDocument和HtmlWindow对象使用

(3)   HtmlWindow的属性简介

       相当于JavaScript中的Window对象,

(4)   操作Cookie

       设置Cookie可以使用

       HtmlPage.Document.SetProperty(“cookie”,cookieValue);

       取得Cookie 使用

       HtmlPage.Document.Cookies;即取得了保存在Cookie中的字符串。

       编写删除Cookie的操作,只要设置Cookie过期时间即可。

(5)   Url和Html的编码问题

      Silverlight中提供一个HttpUtility方法,里面有对应的HtmlEncode、HtmlDecode、UrlEncode和UrlDecode方法。

      HtmlEncode:将文本字符串进行Html编码

      HtmlDecode:将Http传递的html编码字符串转换成文本字符串

      UrlEncode:将文本字符串转换成Url编码字符串

      UrlDecode:将Url编码字符串转换成文本字符串

(6)    取得浏览器信息

      取得HtmlPage.BrowserInformation对象的相关属性,即可取得相应的浏览器的信息

修改DOM:

    HtmlPage.Document.GetElementById("testSpan").SetAttribute("innerText", DateTime.Now.ToString());

 

托管代码调用JavaScript

使用Alert和Confirm方法

bool rst=  HmtlPage.Window.Confirm("确定点击吗?");

GetProperty和CreateInstance方法:

function Testfunc(a, b) 
        {
           this.a=a;
           this.b=b;
           alert(a+b);
       }
       Testfunc.prototype =
       {
           SayHello: function () {
               alert("hello:" + this.a + this.b);
           }
       }  
 ScriptObject myScript = HtmlPage.Window.GetProperty("Testfunc") as ScriptObject;
 myScript.InvokeSelf(textBox1.Text, textBox2.Text);
 ScriptObject myScript = HtmlPage.Window.CreateInstance("Testfunc", textBox1.Text, textBox2.Text);
 myScript.Invoke("SayHello");

 

Eval方法:

HtmlPage.Window.Eval("alert('evalHello"+textBox1.Text+"')");

 

调用JavaScript中的JSON对象:

var person =[ {
        Name:"name1",
        Sex: "1"
         },
        {
             Name: "name2",
             Sex: "2"
         }]
 ScriptObject obj = HtmlPage.Window.GetProperty("Person") as ScriptObject;
       Person[] person = obj.ConvertTo<Person[]>();
       textBox1.Text = person[1].Name;

使用JavaScript调用托管代码

 使用RegisterScriptableObject方法

   public MainPage()
        {
            InitializeComponent();
            HtmlPage.RegisterScriptableObject("Calculator", this);
        }
        [ScriptableMember]
        public double Add(double a, double b)
        {
            return a + b;
        }
     function Button1_onclick() {
           var slPlugin = document.getElementById("MySL");
           alert(slPlugin.content.Calculator.Add("2", "3"));
}

使用RegisterCreateableType方法
 

[ScriptableType]
    public class Calculator
    {
        [ScriptableMember]
        public double Add(double a, double b)
        {
            return a + b;
        }
    }


 HtmlPage.RegisterCreateableType("calculator", typeof(Calculator));

 function Button1_onclick() {
           var slPlugin = document.getElementById("MySL");
           var cal = slPlugin.content.services.createObject("calculator");
           alert(cal.Add("2", "200"));
       }


 使用托管代码处理DOM元素事件

     HtmlPage.Document.GetElementById("id").AttachEvent("onclick",Onclick)

 

 使用Javascript处理托管事件

[ScriptableType]
    public class Cell
    {
        [ScriptableMember]
        public String Key { get; set; }
        [ScriptableMember]
        public String Value { get; set; }
    }
    [ScriptableType]
    public class CellsEventArgs : EventArgs
    {
        [ScriptableMember]
        public Cell[] Cells { get; set; }
    }
    [ScriptableType]
    public class MyCellObject
    {
        public void FireCellsHandle(Cell[] cells)
        {
            if (CellsHandle != null)
            {
                CellsHandle(this, new CellsEventArgs { Cells = cells });
            }
            else
            {
                HtmlPage.Window.Alert("无js处理事件");
            }
        }
        [ScriptableMember]
        public event EventHandler<CellsEventArgs> CellsHandle;
    }
    MyCellObject myobject;
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
       myobject = new MyCellObject(); 
       HtmlPage.RegisterScriptableObject("myobject", myobject);
    }
    private void button4_Click(object sender, RoutedEventArgs e)
    {
       myobject.FireCellsHandle(new Cell[]{
            new Cell{Key="Key1",Value="Value1"},
            new Cell{Key="Key2",Value="Value2"}
          });
    }

 

  function Button1_onclick()
  {
      document.getElementById("MySL").content.myobject.CellsHandle = function (sender, args) {
          alert(sender.toString());
          alert(args.Cells[1].Key);
      } 
  }

通过InitParams传递参数
 
在Silverlight参数中加<param name="initparams" value="MyParam0=test,MyParam1=222 " />

 读取方法:

    在App.xaml中

    private void Application_Startup(object sender, StartupEventArgs e)
         {
            this.RootVisual = new MainPage(e);
         }

    在MainPage中

   public MainPage(StartupEventArgs e)
   {
      InitializeComponent();
      string param ;
      bool exist= e.InitParams.TryGetValue("MyParam1", out param);
      if (exist)
         HtmlPage.Window.Alert(param);
      else
         HtmlPage.Window.Alert("MyParam1不存在");
  }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值