Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (26 votes)
12 Dec 2009Ms-PL5 min read 55.5K   2.2K   56  
This article introduces several Silverlight samples in the All-In-One Framework.
/****************************** Module Header ******************************\
* Module Name:                 MainPage.xaml.cs
* Project:                     CSSL3HTMLBridge
* Copyright (c) Microsoft Corporation.
* 
* HTML bridge samples code behind file.
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* History:
* * 10/21/2009 6:34 PM Mog Liang Created
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace CSSL3HTMLBridge
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            // Register Scriptable Object.
            HtmlPage.RegisterScriptableObject("silverlightPage", this);

            // Attach Html Element event.
            HtmlPage.Document.GetElementById("Text2").AttachEvent("onkeyup", new EventHandler(HtmlKeyUp));
            
            tb1.TextChanged += new TextChangedEventHandler( tb1_TextChanged);
            tb4.TextChanged += tb4_TextChanged;
        }

        // Call javascript when first textbox text changed.
        private void tb1_TextChanged(object sender, TextChangedEventArgs e)
        {
            HtmlPage.Window.Invoke("changetext", tb1.Text);
        }

        // Handle html textbox keyup event.
        private void HtmlKeyUp(object sender, EventArgs e)
        {
                var value = ((HtmlElement)sender).GetProperty("value");
                tb2.Text = value.ToString();
        }

        // Create method for javascript.
        [ScriptableMember]
        public void ChangeTB3Text(string text)
        {
            tb3.Text = text;
        }

        // Create event allow registering by javascript.
        [ScriptableMember]
        public event EventHandler<TextEventArgs> TextChanged;

        void tb4_TextChanged(object sender, TextChangedEventArgs e)
        {
            var myargs = new TextEventArgs();
            myargs.Text = tb4.Text;
            if (TextChanged != null)
                TextChanged(this, new TextEventArgs { Text = tb4.Text });
        }

    }

    // Create custom Eventargs containing text property.
    public class TextEventArgs : EventArgs
    {
        [ScriptableMember]
        public string Text { set; get; }
    }
}

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)


Written By
China China
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/

Comments and Discussions