Silverlight 浏览器交互

迁移时代码部分样式没了,具体见http://blog.csdn.net/ssll7759/article/details/7362405

通过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方法:

js中的定义:

function Testfunc(a, b) 

        {

           this.a=a;

           this.b=b;

           alert(a+b);

       }

       Testfunc.prototype =

       {

           SayHello: function () {

               alert("hello:" + this.a + this.b);

           }

       }  

2种调用方法

 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处理托管事件

  public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }



        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" }

            }); 

        }

    }





    [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;

    } 

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)