Use Silverlight 2.0 (10) JavaScript to call Silverlight

Source: Internet
Author: User

Abstract:Silverlight 2.0 beta1 has been released and many exciting new features have been added: wpf ui framework, rich controls, rich network support, and rich base class library support. This is my study note, which is quite messy, so it is named as the Silverlight 2.0 series.Article.

This section describes how to use JavaScript to call Silverlight.

Scriptabletypeattribute/scriptablememberattribute expose the Silverlight type or method to JavaScript For JavaScript call. You also need to register the type or method of registercreateabletype/registerscriptableobject of htmlpage. The registered method can also use the unregistercreateabletype/unregisterscriptableobject of htmlpage to cancel the registration of the type or method.

Example:(Calculate the number of a certain Fibonacci number in the Fibonacci series)

HTML:
< Div Class = "Container" >
< Div >
< Input Type = "Button" Class = "BTN" Onclick = "Javascript: regtype ();" Value = "Note Book" />
< Input Type = "Button" Class = "BTN" Onclick = "Javascript: unregtype ();" Value = "Remove" />
</ Div >
< Div >
Bits: < Input Class = "TXT" ID = "Txtnum" Type = "Text" />
</ Div >
< Div >
Result: < Input Class = "TXT" ID = "Txtresult" Type = "Text" />
</ Div >
< Div >
< Input Class = "BTN" Value = "Computing" Type = "Button" Onclick = "Javascript: createtype ();" />
</ Div >
< Div ID = "MSG" >
The 'pythonacci' type is not registered.
</ Div >
</ Div >

Javascript:
< Script Type = "Text/JavaScript" >
VaR Slid = " <% = Xaml1.clientid %> " ;

Function $ (Objid)
{
Return Document. getelementbyid (objid );
}

function regtype ()
{< BR >$ (SLID ). content. regmanager. reg ();
$ ( " MSG " ). innertext = " registered 'pythonacci' type " ;< BR >}

function unregtype ()
{< BR >$ (SLID ). content. regmanager. unreg ();
$ ( " MSG " ). innertext = " unregistered 'canonicalcies' type " ;< BR >}

Function Createtype ()
{
VaR F = $ (SLID). content. Services. Createobject ( " Fibonacci " );
VaR Num = $ ( " Txtnum " ). Value;

$ ( " Txtresult " ). Value = F. Fib (Num );
}
</ Script >

Document. getelementbyid ("") gets the Silverlight instance. The content attribute calls the registered method of the type and calls the method of the current Silverlight instance.

XAML:
< Usercontrol X: Class = "Browserrelated. Page"
Xmlns = "Http://schemas.microsoft.com/client/2007"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "40" Loaded = "Usercontrol_loaded" >
< Stackpanel Background = "Darkgreen" >
< Textblock X: Name = "Tbkstatus" Text = "The 'pipeline' type is not registered"
Margin = "30,10" Foreground = "Yellow" > </ Textblock >
</ Stackpanel >
</ Usercontrol >

C #:
Public partial class page: usercontrol
{< br> Public page ()
{< br> initializecomponent ();
}

[scriptablememberattribute]
Public void Reg ()
{< br> htmlpage. registercreateabletype ( " maid " , typeof (Fibonacci)
tbkstatus. text = " registered 'pythonacci' type " ;< BR >}

[scriptablememberattribute]
Public void unreg ()
{< br> htmlpage. unregistercreateabletype ( " maid " );
tbkstatus. text = " the 'pythonacci' type is not registered " ;< BR >}

Private VoidUsercontrol_loaded (ObjectSender, routedeventargs E)
{
Htmlpage. registerscriptableobject ("Regmanager",This);
}
}

[Scriptabletypeattribute]
Public Class Fibonacci
{
[Scriptablememberattribute]
Public Int FIB ( Int Num)
{
Int First = 1 ;
Int Second = 1 ;
Int Third = First + Second;

If (Num = 1 )
{< br> return first;
}

If(Num=2)
{
ReturnSecond;
}

For ( Int I = 3 ; I < Num; I ++ )
{
First = Second;
Second = Third;
Third = First + Second;
}

Return Third;
}
}

Running effect:

As you can see, canceling registration will cause a script error.

The type and method must be public; otherwise, an exception is thrown. It is easy to use and can be understood by referring to the above example.

Conclusion:

Through the introduction in the previous article and this article, we have a preliminary understanding of the interaction between JavaScript and Silverlight. Next, we will introduce other content.

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.