天天看點

使用動态語言來制作silverlight

在silverlight beta 2 中已經支援了動态語言.但是在Visual Studio 和 Experssion Blend中還沒有使用動态語言的模版.我們目前隻可以手動建立. ok  開始吧~

準備工作:

<a href="http://www.codeplex.com/dynamicsilverlight/Release/ProjectReleases.aspx?ReleaseId=11955">Dynamic Silverlight SDK</a>

<a href="http://www.microsoft.com/silverlight/resources/installationFiles.aspx?v=2.0">Silverlight 2 Runtime</a>

<a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=4E03409A-77F3-413F-B108-1243C243C4FE&amp;displaylang=en">Silverlight 2 SDK</a>

建立立一個空網站

使用動态語言來制作silverlight

接下來右鍵點選解決方案選擇添加新項.添加一個html頁面.命名為default.htm

使用動态語言來制作silverlight

編輯該頁面

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;

    &lt;title&gt;start DLR&lt;/title&gt;

    &lt;style type="text/css"&gt;

        html, body

        {

            height: 100%;

            overflow: auto;

        }

        body

            padding: 0;

            margin: 0;

        #silverlightControlHost

    &lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

    &lt;div id="silverlightControlHost"&gt;

        &lt;object data="data:application/x-silverlight," type="application/x-silverlight-2-b1"

            width="100%" height="100%"&gt;

            &lt;param name="source" value="app.xap" /&gt;

            &lt;param name="background" value="white" /&gt;

            &lt;param name="windowless" value="true" /&gt;

        &lt;/object&gt;

        &lt;iframe style='visibility: hidden; height: 0; width: 0; border: 0px'&gt;&lt;/iframe&gt;

    &lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;

注意意中的source參數"app.xap".我們的檔案中并沒有這個檔案.這是由SDK中的Chiron自動生成的.你存放sl代碼的檔案夾也必須叫這個名字"app".其中動态代碼的名字必須是app.比如"app.rb","app.xaml"

在根目錄下建立app檔案夾.再此目錄下添加xml檔案命名為app.xaml.

使用動态語言來制作silverlight

&lt;UserControl

使用動态語言來制作silverlight

    xmlns="http://schemas.microsoft.com/client/2007"

使用動态語言來制作silverlight

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

使用動态語言來制作silverlight

    x:Class="System.Windows.Controls.UserControl"

使用動态語言來制作silverlight

    x:Name="Page"

使用動态語言來制作silverlight

    &gt;

使用動态語言來制作silverlight

  &lt;TextBlock

使用動态語言來制作silverlight

     x:Name="txtMessage" TextWrapping="Wrap"

使用動态語言來制作silverlight

     Foreground="Black" Text="Hello World" &gt;

使用動态語言來制作silverlight

  &lt;/TextBlock&gt;

使用動态語言來制作silverlight

&lt;/UserControl&gt;

這裡以ruby為例.在app目錄下添加一個text檔案.命名為app.rb

使用動态語言來制作silverlight

到了這一步就可以開始寫rb的代碼了.

include System::Windows

include System::Windows::Controls

include System::Windows::Media

class SilverlightApplication

  def application

    Application.current

  end

  def self.use_xaml(options = {})

    options = {:type =&gt; UserControl, :name =&gt; "app"}.merge(options)

    Application.current.load_root_visual(options[:type].new, "#{options[:name]}.xaml")

  def root

    application.root_visual

  def method_missing(m)

    root.send(m)

end

class FrameworkElement

    find_name(m.to_s.to_clr_string)

class App &lt; SilverlightApplication

  use_xaml

  def initialize

    txtMessage.text = "Welcome to Ruby in Silverlight"

App.new

為了運作起來需要在vs中設定一下.

在網站屬性頁中的啟動選項選擇"啟動外部程式",選中sdk中的"Chiron.exe".指令行參數為"/b".工作目錄設定為項目所在目錄.

使用動态語言來制作silverlight

按F5運作程式

使用動态語言來制作silverlight
使用動态語言來制作silverlight

浏覽器打開http://localhost:2060.在這裡你可以用目錄浏覽的方式檢視檔案.

點選default.htm

使用動态語言來制作silverlight

顯示了"Welcome to Ruby in Silverlight".這是由rb檔案控制的.

修改代碼.

    #txtMessage.text = "Welcome to Ruby in Silverlight"

注釋掉設定文本的語句.

按F5重新整理浏覽器

使用動态語言來制作silverlight

顯示"Hello World".這是xaml自己描述的.

ok  介紹完畢.

<a href="http://files.cnblogs.com/nasa/silverlight/start.zip">http://files.cnblogs.com/nasa/silverlight/start.zip</a>

使用動态語言來制作silverlight

繼續閱讀