はじめに
前回はC#でのSilverlight 2の開発をご紹介しました。今回は.NET上で動的言語を実行するDynamic Language Runtime
Dynamic Silverlightとは
最初に少し書きましたが、
DSLでは現在、
- IronPython
- IronRuby
- Managed JScript
今後はさらに様々なDLR上で動く言語が登場し、
SDKの導入
DSLにはDSL用のSDKが用意されています。以下のサイトよりダウンロードしてください。
ダウンロードが完了したら、
- DSLランタイムやIronRuby/
IronPythonなどのアセンブリ - webサーバーとしての実行とxapの生成を行うChiron.
exe - 上記のソースコード
- サンプルアプリケーション
実は、
- Silverlight 2 SDKに含まれるバージョン = 2.
0.5. 0 - DSL SDKに含まれるバージョン = 2.
0.5. 100
今回はDSLのSDKを使用して解説を行います。
開発ツールのChiron.exe
Chiron.
- 開発用の簡易的なWebサーバー
- xapファイルの生成
DSLの開発ではChiron.
Chiron.exe -h
Macでの開発
Chiron.
本稿でもMacを使用して解説を行います。ただし、
mono Chiron.exe -h
Chiron.exe -h
Monoのダウンロードと導入は以下のサイトの手順に従って行ってください。
DSLのSDKもZIP形式で圧縮されているので、
mono Chiron.exe -h
IronRubyでHello world
では、
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Dynamic Silverlight Sample</title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
}
</style>
</head>
<body>
<div id="silverlightControlHost">
<object data="data:application/x-silverlight,"
type="application/x-silverlight-2-b1"
width="100%" height="100%">
<param name="source" value="app.xap"/>
</object>
</div>
</body>
</html>
続いては、
<UserControl
xmlns=http://schemas.microsoft.com/client/2007
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
x:Class="System.Windows.Controls.UserControl"
x:Name="Page">
<TextBlock x:Name="TextBlock1" Text="Hello world."
FontSize="24" />
</UserControl>
これでひな形が完成しました。上記のindex.
では、
Include System::Windows
Include System::Windows::Controls
control = Application.current.load_root_visual UserControl.new, 'app.xaml'
control.find_name('TextBlock1').mouse_left_button_up do |sender,args|
sender.text = 'Hello world for IronRuby!'
end
作成できたらターミナルでカレントディレクトリをindex.
mono Chiron.exe /b
以下のようなディレクトリの画面が表示ます。

index.

ここで、

IronPythonでもHello world
前回からHello world続きですが、
index.
from System.Windows import Application
from System.Windows.Controls import UserControl
def OnClick(sender, args):
sender.Text = "Hello world for IronPython!"
control = Application.Current.LoadRootVisual(UserControl(), "app.xaml")
control.TextBlock1.MouseLeftButtonDown += OnClick
では、
index.

IronRubyをもう少し簡単(?)に
すこし、
Rubyではmethod_
class FrameworkElement
def method_missing(m)
find_name(m.to_s.to_clr_string)
end
end
FrameworkElementクラスでmethod_
Include System::Windows
Include System::Windows::Controls
class FrameworkElement
def method_missing(m)
find_name(m.to_s.to_clr_string)
end
end
control = Application.current.load_root_visual UserControl.new, 'app.xaml'
control.TextBlock1.mouse_left_button_up do |sender,args|
sender.text = 'Hello world for IronRuby!'
end
アプリケーションが実行されるまで
さて、
- index.
htmlを作成 - app.
xamlを作成 - app.
rbもしくはapp. pyを作成 - Chiron.
exe /b で実行
これらの手順のなかのどこにもxapファイルを作成する手順がありません。しかし、
実はChiron.
- 読み込むxapファイルの拡張子を除いたファイル名のディレクトリを検索
(今回の例ではapp) - 1.のディレクトリの中のファイルの拡張子から判断して、
含めるDLRのアセンブリを決定し、 AppManifest. xamlを生成 - 1.のディレクトリと2.のAppManifest.
xamlをxapファイルにパッケージング
上記で作成された、
では、
これは、
このappという値を変更するにはSilverlightプラグインを生成するとのinitParamsにstartというキーで値を指定します。たとえば
<param name="initParams" value="start=test" />
これらの内容はすべてソースコードから読み取ることができます。たしかにソースコードを読み込むことはなかなか大変なことですが、
http://localhost:2060/bye!
次回予告
記事としては本当に簡単なコードしか書いていませんが、
さて次回は、