<StackPanel Background="#CDFCAE" Orientation="Horizontal"> <Border CornerRadius="10" Width="100" Height="40" Margin="50 10 0 0"> <TextBlock Text="结果显示:" FontSize="20" Foreground="Red"></TextBlock> </Border> <Border CornerRadius="10" Background="Green" Width="300" Height="40"> <TextBlock x:Name="result" FontSize="20" Foreground="White" Margin="20 5 0 0"></TextBlock> </Border> </StackPanel>
private void UserControl_Loaded(object sender, RoutedEventArgs e) { HtmlPage.RegisterScriptableObject("Calculator", this); }
[ScriptableMember] public void Add(int x, int y) { int z = x + y; this.result.Text = String.Format("{0} + {1} = {2}", x, y, z); }
<div class="main"> <input id="txt1" type="text" /> <input id="txt2" type="text" /> <input id="Button1" type="button" value="确 定"/> </div>
<script type="text/javascript"> function callSilverlight() { var slPlugin = $get('Xaml1'); slPlugin.content.Calculator.Add($get('txt1').value,$get('txt2').value); } </script>
<input id="Button1" type="button" value="确 定" onclick="callSilverlight()" />
[ScriptableType] public class Calculator { [ScriptableMember] public int Add(int x, int y) { return x + y; } }
HtmlPage.RegisterCreateableType("calculator", typeof(Calculator));
<script type="text/javascript"> function callSilverlight() { var slPlugin = $get('Xaml1'); var cal = slPlugin.content.services.createObject("calculator"); alert(cal.Add($get('txt1').value,$get('txt2').value)); } </script>
本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67266
本文出自 51CTO.COM技术博客