通过System.Windows.browser命名空间下的HtmlPage,HtmlDocument,HtmlElement,HtmlWindow操作浏览器对象
(1) HtmlDocument的属性简介
Body:Html的Body对象
Cookies:Cookie字符串
DocumentElement:
DocumentUri:Silverlight宿主的html地址
(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.browser@R_157_4045@ion对象的相关属性,即可取得相应的浏览器的信息
修改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;
使用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不存在"); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。