<div id="result"></div>
<script type="text/javascript"> function Hello(message) { var resultSpan = $get("result"); resultSpan.innerText = "Hello " + message; } </script>
<StackPanel Background="#CDFCAE" Orientation="Vertical"> <StackPanel Height="40"> <TextBlock Text="Calling Browser Script from Silverlight" Foreground="Red"></TextBlock> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBox x:Name="input" Width="340" Height="40" Margin="20 0 20 0"></TextBox> <Button x:Name="submit" Width="120" Height="40" Background="Red" Content="调 用" FontSize="20" Foreground="Red" Click="submit_Click"></Button> </StackPanel> </StackPanel>
private void submit_Click(object sender, RoutedEventArgs e) { ScriptObject hello = HtmlPage.Window.GetProperty("Hello") as ScriptObject; hello.InvokeSelf(this.input.Text); }
[SecuritySafeCritical] public virtual object Invoke(string name, params object[] args); [SecuritySafeCritical] public virtual object InvokeSelf(params object[] args);
<script type="text/javascript"> myHello = function(message) { this.Message = message; } myHello.prototype.Display = function() { var resultSpan = $get("result"); resultSpan.innerText = "Hello " + this.Message; } </script>
private void submit_Click(object sender, RoutedEventArgs e) { ScriptObject script = HtmlPage.Window.CreateInstance("myHello",this.input.Text); object result = script.Invoke("Display"); }
private void submit_Click(object sender, RoutedEventArgs e) { HtmlPage.Window.Eval(this.input.Text); }
private void submit_Click(object sender, RoutedEventArgs e) { HtmlElement result = HtmlPage.Window.Eval("document.getElementById('result')") as HtmlElement; string message = result.GetAttribute("innerHTML"); HtmlPage.Window.Alert(message); }
<script type="text/javascript"> function myHello(message) { $("#result").text("Hello " + message); } </script>
private void submit_Click(object sender, RoutedEventArgs e) { ScriptObject script = HtmlPage.Window.GetProperty("myHello") as ScriptObject; script.InvokeSelf(this.input.Text); }
本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67264
本文出自 51CTO.COM技术博客