“Silverlight”中获取“HTML元素和参数”及JS交互
1、获取HTTP连接中的参数
http://localhost:5526/sl.aspx?user=root&password=root
HtmlPage.Document.QueryString
取到的是一个字典IDictionary<string, string>
key=user,value=root
key=password,value=root
2、从“<param name="initparams" value="ChartType=WaterMonitoring" />”中获取“initparams”参数
e.InitParams["ChartType"].ToString();
3、“HTML:<input type="hidden" name="hf_qty" id="hf_qty" value="705.5" />”中获取“input元素”
HtmlDocument doc = HtmlPage.Document;
HtmlElement el = doc.GetElementById("hf_qty");
var value = el.GetProperty("value");
4、“与JavaScript交互获取”
首先在HTML页面写一个JavaScript:
<script type="text/javascript">
function GetValue() {
return document.getElementById("hf_qty").value;
}
</script>
在Silverlight代码里:
var value = HtmlPage.Window.Invoke("GetValue");
5、“Silverlight”中操作“HTML”元素
HTML:
<div id="myDIV" style="background:blue;width:20%;height:20%">
<asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/SilverlightApplication27.xap" MinimumVersion="2.0.30930.0"
Width="50%" Height="100%" />
</div>
Silverlight:
private void Button_Click(object sender, RoutedEventArgs e)
{
HtmlDocument doc = HtmlPage.Document;
HtmlElement div = doc.GetElementById("myDIV");
div.SetStyleAttribute("background", "green");
}