1JavaScript访问silverlight函数
代码
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.RegisterScriptableObject("SilverlightLearn", this);
}
[ScriptableMember]
public void SayHelloToMe(string firstName)
{
Block1.Text = string.Format("Hello {0}", firstName);
}
代码
<script type="text/javascript">
function sayHello(fname)
{
var slhost = document.getElementById("Xaml1");
slhost.content.SilverlightLearn.SayHelloToMe(fname);
}
function buttonClick()
{
var f = document.getElementById("HtmlText1").value;
sayHello(f);
}
</script>
<input onclick="buttonClick();" type="button" id="htmlButton1" value="Html Button" />
2 silverlighty页访问JavaScript函数
<script type="text/javascript">
function sayGoodbye(fname)
{
var box = document.getElementById("HtmlText1");
box.value = fname;
}
</script>
代码
//第一种方式
ScriptObject jsObj = (ScriptObject)HtmlPage.Window.GetProperty("sayGoodbye");
jsObj.InvokeSelf(TextBox1.Text);
// 第二种方式
HtmlPage.Window.Invoke("sayGoodbye", "Tim", "Heuer" );