Silverlight 2 Beta 1を入れてみた

Silverlight2のBeta1とSDKがリリースされたので、とりあえず入れてみた。

VS2008で開発するためのツールはExpress Editionには対応していないとのこと。しばらくはSDKだけでがんばる。

SDKといっしょにダウンロードしてきたドキュメントに「Dynamic Languages in Silverlight 2」という項目があったので、そこのコードをコピーして動かしてみる。言語はIronPython

ドキュメントによるとソースファイルとXMALファイルは「app」というフォルダに入れておく必要があるとのこと。

フォルダ構成
[SilverlightApplication]
  |
  |-- [app]
  |     |-- app.py
  |     |-- app.xaml
  |
  |-- index.htm

エントリーポイントになるクラスはApplicationクラスから派生する必要がある。

app.py
from System.Windows import Application

class App:
   def __init__(self):
      self.scene = Application.Current.LoadRootVisual("app.xaml")
   def start(self):
      # TO DO: Replace this with your application start logic.
      self.scene.Message.Text = "Welcome to Silverlight and IronPython!"

App().start()

初期画面

app.xaml
<Canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="System.Windows.Controls.Canvas"
  x:Name="Page"
  >
  
  <TextBlock x:Name="Message" TextWrapping="Wrap" Foreground="White" />
</Canvas>

SilverlightをホストするHTML画面

index.htm

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>Silverlight Dynamic Language Template</title>
   <!-- Silverlight bootstrapping -->
   <script src="assets/js/silverlight.js" type="text/javascript" ></script>

   <style type="text/css">
      body {
        background-color: black;
      }
   </style>
</head>

<body>
  <div id="SilverlightControlHost" onload="self.focus()">
    <script type="text/javascript">
      var controlID = "SilverlightControl"
      Silverlight.createObjectEx({
        source: "app.xap",
        parentElement: document.getElementById("SilverlightControlHost"),
        id: controlID,
        properties: {
          width:'500px', 
          height:'100px',
          inplaceInstallPrompt: false,
          background:'Black',
          isWindowless: 'False',
          version:'1.1'
        },
        events: {},
        initParams: "debug=true"
      });
    </script>
  </div>

</body>
</html>

あとはコマンドプロンプトを起動して、「SilverlightApplication」フォルダに移動する。
そしてSilverlight SDKインストールフォルダの「Tools\Chiron」フォルダにある「Chiron.exe」(なんだこの名前?)に「/b」オプションを付けて起動する。

PS > Chiron.exe /b

これでSilverlightをホストするWebサーバーが起動するので、ブラウザで「http://localhost:2060/」にアクセスすると、

Message: Microsoft.Scripting.ArgumentTypeException: LoadRootVisual() takes exactly 1 argument (1 given)
・・・

なんていうエラーメッセージが表示された。どうやら「LoadRootVisual」というメソッドに渡す引数が足りないようだ。
↓この部分

self.scene = Application.Current.LoadRootVisual("app.xaml")

で、リファレンスやらオンラインのチュートリアルやらを見てみると全然違うこと書いてあるし。。。orz

正しく書き直したのがこちら

app.py
from System.Windows import Application
from System.Windows.Controls import Canvas

class App:
   def __init__(self):
      self.scene = Application.Current.LoadRootVisual(Canvas(), "app.xaml")
   def start(self):
      # TO DO: Replace this with your application start logic.
      self.scene.Message.Text = "Welcome to Silverlight and IronPython!"

App().start()

どうやら「LoadRootVisual」メソッドの第一引数には、第二引数で渡すXAMLの基底クラスをインスタンス化して渡す必要があるらしい。ここでは「app.xaml」はCanvasから作っているので、それをインスタンス化して渡した。

これで・・・おお!!ちゃんと動いた!!

「index.htm」の「Silverlight.createObjectEx」メソッドの引数では「source」というパラメータに「app.xap」(ザップ?)というの渡しているけど、実際にはそんなファイルは存在していない。それでも何故動作するかといえば、実行時に「app」というフォルダから「on the fly」で作っているらしい。ふ〜ん。

これだけではつまらないので、Silverlight2から追加されたコントロールを使ってみる。

簡単なところで「Button」から。

app.xaml
<Canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="System.Windows.Controls.Canvas"
  x:Name="Page"
  >
  
  <Button x:Name="button" Text="Hello" />
</Canvas>

これで動かしてみると・・・

Message: System.Windows.Markup.XamlParseException: Unknown element: Button. [Line: 8 Position: 4]
・・・

なんてエラーがまた出やがった。

いろいろ調べてみると、どうやら「Button」なんかの新しいコントロールSDKをインストールすると入る「System.Windows.Controls.dll」アセンブリに入っているんだけど、これを参照することができていないっぽい。

このアセンブリを参照することが出来れば、動きそうだけどやり方がわからない。「AppManifest.xml」とやらを作って参照してみたけど効果なしなんで、あきらめた。

AmazonでVS2008 Standard Editionを注文したので、届いてから続きをやることにする。

Visual Studio 2008 Standard Edition アップグレード

Visual Studio 2008 Standard Edition アップグレード