<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msdnvietnam.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Silverlight Tip of the Day #15 – Communicating between JavaScript &amp; Silverlight</title><link>http://msdnvietnam.net/wikis/silverlighttips/silverlight-tip-of-the-day-15-communicating-between-javascript-amp-silverlight.aspx</link><description>Các mẹo khi lập trình SilverLight</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Silverlight Tip of the Day #15 – Communicating between JavaScript &amp; Silverlight</title><link>http://msdnvietnam.net/wikis/silverlighttips/silverlight-tip-of-the-day-15-communicating-between-javascript-amp-silverlight.aspx</link><pubDate>Sat, 23 May 2009 00:21:56 GMT</pubDate><guid isPermaLink="false">019a604b-fb74-42d5-8a6d-7cedfb08f61c:32</guid><dc:creator>Nguyễn Thị Như Quỳnh</dc:creator><comments>http://msdnvietnam.net/wikis/silverlighttips/silverlight-tip-of-the-day-15-communicating-between-javascript-amp-silverlight/comments.aspx</comments><description>Current revision posted to SilverLight programming tip tricks by Nguyễn Thị Như Quỳnh on 5/23/2009 8:21:56 AM&lt;br /&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Rất may mắn l&amp;agrave; sự giap tiếp giữa Silverlight v&amp;agrave; Javascript&amp;nbsp; tương đối kh&amp;ocirc;ng phức tạp cho lắm. V&amp;iacute; dụ dưới đ&amp;acirc;y sẽ minh chứng cho bạn biết phải l&amp;agrave;m như thế n&amp;agrave;o để sử dụng 2 c&amp;aacute;ch gọi.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Gọi Silverlight Từ Java script:&lt;/b&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Trong h&amp;agrave;m khởi tạo của Silverlight app, thực hiện gọi đến
RegisterScriptableObject (). Bản chất của phương thức gọi n&amp;agrave;y l&amp;agrave; đăng
k&amp;iacute; một đối tượng quản l&amp;yacute; cho kịch bản kết nối (scriptable access) bởi
m&amp;atilde; Javascript.. Tham số đầu ti&amp;ecirc;n c&amp;oacute; thể t&amp;ugrave;y &amp;yacute; đặt từ kh&amp;oacute;a theo &amp;yacute; của bạn.
Từ kh&amp;oacute;a n&amp;agrave;y sẽ tham chiếu đến m&amp;atilde;&amp;nbsp; Javascript của bạn khi thực hiện phương thức gọi
Silverlight. &lt;/li&gt;
&lt;li&gt;Th&amp;ecirc;m fuction m&amp;agrave; bạn muốn gọi trong code Silverlight của bạn. Bạn n&amp;ecirc;n th&amp;ecirc;m n&amp;oacute; với thuộc t&amp;iacute;nh&amp;nbsp; &lt;span style="color:#800000;"&gt;[ScriptableMember]&lt;/span&gt; ở đầu. &lt;/li&gt;
&lt;li&gt;Trong Javascript, bạn c&amp;oacute; thể gọi trực tiếp đến function nằm trong code Silverlight của bạn . Điều n&amp;agrave;y c&amp;oacute; thể thực hiện được bởi document object. Từ th&amp;iacute; dụ của t&amp;ocirc;i sau đ&amp;acirc;y: &lt;span style="color:#0000ff;"&gt;document.getElementById(&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;quot;silverlightControl&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;).&lt;span style="color:#0000ff;"&gt;Content.Page.UpdateText(&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;quot;Hello from Javascript!&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;); &lt;span style="color:#000000;"&gt;nơi&amp;nbsp; &amp;ldquo;silverlightControl&amp;rdquo; l&amp;agrave;&amp;nbsp; ID của Silverlight control của t&amp;ocirc;i.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&lt;b&gt;Gọi Javascript Từ Silverlight:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;Javascript c&amp;oacute; thể được gọi trực tiếp trong &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;function&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;HtmlPage&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;.Window.Invoke() &lt;span style="color:#000000;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;Th&amp;iacute; dụ cho cả hai c&amp;aacute;ch tr&amp;ecirc;n:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;Page.xaml:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; SilverlightApplication&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Page : UserControl&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Page()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            InitializeComponent();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            HtmlPage.RegisterScriptableObject(&lt;span style="color:#006080;"&gt;&amp;quot;Page&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;);            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            HtmlPage.Window.Invoke(&lt;span style="color:#006080;"&gt;&amp;quot;TalkToJavaScript&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Hello from Silverlight&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [ScriptableMember]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; UpdateText(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; result)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            myTextbox.Text = result;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Default.aspx:&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&amp;lt;script type=&lt;span style="color:#006080;"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&amp;gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        function TalkToJavaScript( data)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            alert(&lt;span style="color:#006080;"&gt;&amp;quot;Message received from Silverlight: &amp;quot;&lt;/span&gt;+data);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var control = document.getElementById(&lt;span style="color:#006080;"&gt;&amp;quot;silverlightControl&amp;quot;&lt;/span&gt;);            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            control.Content.Page.UpdateText(&lt;span style="color:#006080;"&gt;&amp;quot;Hello from Javascript!&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }    &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Page.xaml:&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&amp;lt;UserControl x:Class=&lt;span style="color:#006080;"&gt;&amp;quot;SilverlightApplication7.Page&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    xmlns=&lt;span style="color:#006080;"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    xmlns:x=&lt;span style="color:#006080;"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    Width=&lt;span style="color:#006080;"&gt;&amp;quot;400&amp;quot;&lt;/span&gt; Height=&lt;span style="color:#006080;"&gt;&amp;quot;300&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &amp;lt;Grid x:Name=&lt;span style="color:#006080;"&gt;&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; Background=&lt;span style="color:#006080;"&gt;&amp;quot;White&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &amp;lt;TextBlock x:Name=&lt;span style="color:#006080;"&gt;&amp;quot;myTextbox&amp;quot;&lt;/span&gt;&amp;gt;Waiting &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; call...&amp;lt;/TextBlock&amp;gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &amp;lt;/Grid&amp;gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;lt;/UserControl&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Thank you, 
  &lt;br /&gt;--Mike Snow 
  &lt;/p&gt;</description></item></channel></rss>
