Silverlight 浏览器交互

Silverlight 浏览器交互,第1张

概述通过System.Windows.Browser命名空间下的HtmlPage,HtmlDocument,HtmlElement,HtmlWindow *** 作浏览器对象  (1)  HtmlDocument的属性简介         Body:Html的Body对象         Cookies:Cookie字符串         DocumentElement:         DocumentUr

通过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不存在");  }
总结

以上是内存溢出为你收集整理的Silverlight 浏览器交互全部内容,希望文章能够帮你解决Silverlight 浏览器交互所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1069320.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存