Support for HTML, client script, and so on is built into Silverlight. In the previous article, invoking JavaScript in Silverlight, I'll describe how to invoke. NET code in Silverlight with JavaScript.
Using RegisterScriptableObject
The following two types are available in Silverlight 2:
ScriptableMemberAttribute: Allow us to expose members to script in Silverlight.
Scriptabletypeattribute: Allows us to expose types to script in Silverlight.
At the same time, HtmlPage provides a registercreateabletype and RegisterScriptableObject method for registering types or object instances that can be used by the script. With these, you can invoke Silverlight in JavaScript.
Looking at a simple example, in this example, we expect to pass two parameters to the Silverlight method through JavaScript, which is displayed in Silverlight when the result is calculated, as shown in the figure:
First we write the interface layout in Silverlight:
<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>