Learning Silverlight 2 Series (22): Calling. NET code in Silverlight with JavaScript

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.