PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSSL3HTMLBridge/CSSL3HTMLBridge/MainPage.xaml.cs

#
C# | 90 lines | 57 code | 11 blank | 22 comment | 2 complexity | 51c0cf8bddb3aea4673d81035392ba31 MD5 | raw file
 1/****************************** Module Header ******************************\
 2Module Name:  MainPage.xaml.cs
 3Project:      CSSL3HTMLBridge
 4Copyright (c) Microsoft Corporation.
 5
 6HTML bridge samples code behind file.
 7
 8This source is subject to the Microsoft Public License.
 9See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10All other rights reserved.
11
12THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
13EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
14WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15\***************************************************************************/
16
17using System;
18using System.Collections.Generic;
19using System.Net;
20using System.Windows;
21using System.Windows.Controls;
22using System.Windows.Documents;
23using System.Windows.Input;
24using System.Windows.Media;
25using System.Windows.Media.Animation;
26using System.Windows.Shapes;
27using System.Windows.Browser;
28using System.Runtime.Serialization;
29using System.ComponentModel;
30
31namespace CSSL3HTMLBridge
32{
33    public partial class MainPage : UserControl
34    {
35        public MainPage()
36        {
37            InitializeComponent();
38
39            // Register Scriptable Object.
40            HtmlPage.RegisterScriptableObject("silverlightPage", this);
41
42            // Attach Html Element event.
43            HtmlPage.Document.GetElementById("Text2").AttachEvent("onkeyup", new EventHandler(HtmlKeyUp));
44
45            tb1.TextChanged += new TextChangedEventHandler(tb1_TextChanged);
46            tb4.TextChanged += tb4_TextChanged;
47        }
48
49        // Call javascript when first textbox text changed.
50        private void tb1_TextChanged(object sender, TextChangedEventArgs e)
51        {
52            HtmlPage.Window.Invoke("changetext", tb1.Text);
53        }
54
55        // Handle html textbox keyup event.
56        private void HtmlKeyUp(object sender, EventArgs e)
57        {
58            var value = ((HtmlElement)sender).GetProperty("value");
59            tb2.Text = value.ToString();
60        }
61
62        // Create method for javascript.
63        [ScriptableMember]
64        public void ChangeTB3Text(string text)
65        {
66            tb3.Text = text;
67        }
68
69        // Create event allow registering by javascript.
70        [ScriptableMember]
71        public event EventHandler<TextEventArgs> TextChanged;
72
73        void tb4_TextChanged(object sender, TextChangedEventArgs e)
74        {
75            var myargs = new TextEventArgs();
76            myargs.Text = tb4.Text;
77            if (TextChanged != null)
78            {
79                TextChanged(this, new TextEventArgs { Text = tb4.Text });
80            }
81        }
82    }
83
84    // Create custom Eventargs containing text property.
85    public class TextEventArgs : EventArgs
86    {
87        [ScriptableMember]
88        public string Text { set; get; }
89    }
90}