Support for HTML, client script, and so on is built into Silverlight. In many cases, the Web applications we write use some JavaScript or Ajax frameworks, and we want to be able to invoke certain scripting methods in Silverlight, or trigger the execution of a script in Silverlight. You need to invoke JavaScript in Silverlight, as this article briefly describes.
Using GetProperty to get script objects
Let's look at a simple example of putting a div in the Silverlight test page as a display message:
<div id="result"></div>
Write a simple JavaScript code:
<script type="text/javascript">
function Hello (message)
{
var resultSpan = $get("result");
resultSpan.innerText = "Hello " + message;
}
</script>
Then write a simple input information interface:
<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>