Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

An Introduction to the Silverlight samples in the All-In-One Framework

, 12 Dec 2009
This article introduces several Silverlight samples in the All-In-One Framework.
SilverlightIntroduction_src.zip
CSSL3LocalMessage
Properties
CSSL3MediaElement
CSSL3MediaElement
Properties
CSSL3MediaElement.Web
App_Data
ClientBin
CSSL3MediaElement.xap
Properties
ReadMe.mht
CSSL3PixelShader
cyclewatery.ps
Humpback Whale.jpg
Properties
CSSL3WriteableBitmap
Heart Empty.png
Properties
VBSL3Animation
My Project
VBSL3DeepZoom
VBSL3DeepZoom
images
home_hover.png
home_pressed.png
home_rest.png
My Project
Service References
DeepZoomServiceReference
configuration.svcinfo
configuration91.svcinfo
GenerateDeepZoomService.disco
GenerateDeepZoomService.wsdl
Reference.svcmap
ServiceReferences.ClientConfig
VBSL3DeepZoom.Web
App_Code
App_Data
Bin
DeepZoomTools.DLL
DeepZoomTools.DLL.refresh
ClientBin
GeneratedImages
GenerateDeepZoomService.svc
SourceImages
atom.png
background.jpg
cell.png
dna.png
Molecular.png
neutron.png
seed.png
wrap in bamboo.png
VBSL3HTMLBridge
VBSL3HTMLBridge
My Project
VBSL3HTMLBridge.Web
App_Data
ClientBin
VBSL3HTMLBridge.xap
My Project
Application.myapp
MyExtensions
Settings.settings
VBSL3Input
My Project
VBSL3LocalMessage
My Project
VBSL3MediaElement
ReadMe.mht
VBSL3MediaElement
My Project
VBSL3MediaElement.Web
App_Data
ClientBin
VBSL3MediaElement.xap
My Project
Application.myapp
MyExtensions
Settings.settings
VBSL3PixelShader
cyclewatery.ps
Humpback Whale.jpg
My Project
CSSL3Animation
Properties
CSSL3DeepZoom
CSSL3DeepZoom
images
home_hover.png
home_pressed.png
home_rest.png
Properties
Service References
DeepZoomServiceReference
configuration.svcinfo
configuration91.svcinfo
GenerateDeepZoomService.disco
GenerateDeepZoomService.wsdl
GenerateDeepZoomService1.wsdl
Reference.svcmap
ServiceReferences.ClientConfig
CSSL3DeepZoom.Web
App_Code
Bin
DeepZoomTools.DLL
DeepZoomTools.DLL.refresh
ClientBin
GeneratedImages
GenerateDeepZoomService.svc
SourceImages
atom.png
background.jpg
cell.png
dna.png
Molecular.png
neutron.png
seed.png
wrap in bamboo.png
ReadMe.mht
CSSL3HTMLBridge
CSSL3HTMLBridge
Properties
CSSL3HTMLBridge.Web
App_Data
ClientBin
CSSL3HTMLBridge.xap
Properties
CSSL3Input
Properties
========================================================================
    SILVERLIGHT APPLICATION : CSSL3HTMLBridge Project Overview
========================================================================

/////////////////////////////////////////////////////////////////////////////
Use:

This project create a group of samples demonstrating the interactivity between
silverlight application and javascript. The sample includes:
    Call Javascript method from managed code
    Handle Html event from managed code
    Call managed code method from javascript
    Handle managed code event from javascript


/////////////////////////////////////////////////////////////////////////////
Prerequisites:

Silverlight 3 Tools for Visual Studio 2008 SP1
http://www.microsoft.com/downloads/details.aspx?familyid=9442b0f2-7465-417a-88f3-5e7b5409e9dd&displaylang=en

Silverilght 3 runtime:
http://silverlight.net/getstarted/


/////////////////////////////////////////////////////////////////////////////
Code Logic:

Sample 1: Call Javascript method from managed code
    1. Write javascript method and put in header of silverlight hosted page.
        function changetext(name) {
            document.getElementById('Text1').value = name;
        }
        
    2. Invoke javascript method in managed code.
        HtmlPage.Window.Invoke("changetext", tb1.Text);

Sample 2: Handle Html event from managed code
    1. Attach html event in MainPage Loaded event handler.
        HtmlPage.Document.GetElementById("Text2").AttachEvent("onkeyup", new EventHandler(HtmlKeyUp));
    
    2. Write handler method "HtmlKeyUp".
        private void HtmlKeyUp(object sender, EventArgs e)
        {
                var value = ((HtmlElement)sender).GetProperty("value");
                tb2.Text = value.ToString();
        }

Sample 3: Call managed code method from javascript
    1. Write method in MainPage code behind and apply ScriptableMember attribute
        [ScriptableMember]
        public void ChangeTB3Text(string text)
        {
            tb3.Text = text;
        }
        
    2. Register ScriptableObject instance in MainPage Loaded event.
        HtmlPage.RegisterScriptableObject("silverlightPage", this);

    3. Write javascript event handler for html textbox onchanged event. In handler function, call managed code.
        function ontext3keydown() {
            var text = document.getElementById('Text3').value;
            // calling managed code method
            var silverlight1 = document.getElementById('silverlight1');
            silverlight1.Content.silverlightPage.ChangeTB3Text(text);
        }
    
Sample 4: Handle managed code event from javascript
    1. Write custom EventArgs for transmitting TextBox.Text value.
        public class TextEventArgs : EventArgs
        {
            [ScriptableMember]
            public string Text { set; get; }
        }
        
    2. Write Event in MainPage code behind.
        [ScriptableMember]
        public event EventHandler<TextEventArgs> TextChanged;
    
    3. Fire event when silverlight TextBox.Text changed.
        void tb4_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (TextChanged != null)
                TextChanged(this, new TextEventArgs { Text = tb4.Text });
        }

    4. Register ScriptableObject instance in MainPage Loaded event.
        HtmlPage.RegisterScriptableObject("silverlightPage", this);
        
    5. In silverlight hosted html page, attaching Silverlight loaded event.
        <param name="onLoad" value="onSilverlightLoaded" />

    6. In silverlight loaded eventhandler, handle managed code event.
        function onSilverlightLoaded() {
            var silverlight1 = document.getElementById('silverlight1');
            silverlight1.Content.silverlightPage.TextChanged = function(sender, e) {
                document.getElementById('Text4').value = e.Text;
            }
        }        


/////////////////////////////////////////////////////////////////////////////
References:

HTML Bridge: Interaction Between HTML and Managed Code
http://msdn.microsoft.com/en-us/library/cc645076(VS.95).aspx


/////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Microsoft All-In-One Code Framework delineates the framework and skeleton of Microsoft development techniques through typical sample codes in three popular programming languages (Visual C#, VB.NET, Visual C++). Each sample is elaborately selected, composed, and documented to demonstrate one frequently-asked, tested or used coding scenario based on our support experience in MSDN newsgroups and forums. If you are a software developer, you can fill the skeleton with blood, muscle and soul. If you are a software tester or a support engineer like us, you may extend the sample codes a little to fit your specific test scenario or refer your customer to this project if the customer's question coincides with what we collected.
http://cfx.codeplex.com/

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 12 Dec 2009
Article Copyright 2009 by All-In-One Code Framework
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid