使用动态语言来制作silverlight

在silverlight beta 2 中已经支持了动态语言.但是在Visual Studio 和 Experssion Blend中还没有使用动态语言的模版.我们目前只可以手动建立. ok  开始吧~
准备工作:
Dynamic Silverlight SDK
Silverlight 2 Runtime
Silverlight 2 SDK

新建立一个空网站

接下来右键点击解决方案选择添加新项.添加一个html页面.命名为default.htm

编辑该页面
<!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>start DLR</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" />
            
<param name="background" value="white" />
            
<param name="windowless" value="true" />
        
</object>
        
<iframe style='visibility: hidden; height: 0; width: 0; border: 0px'></iframe>
    
</div>
</body>
</html>

这种写法的原因你可以看看我的这篇文章:"有趣 不用js也能创建silverlight"

注意意中的source参数"app.xap".我们的文件中并没有这个文件.这是由SDK中的Chiron自动生成的.你存放sl代码的文件夹也必须叫这个名字"app".其中动态代码的名字必须是app.比如"app.rb","app.xaml"

在根目录下建立app文件夹.再此目录下添加xml文件命名为app.xaml.
<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="txtMessage" TextWrapping="Wrap"
     Foreground
="Black" Text="Hello World" >
  
</TextBlock>
</UserControl>

这里以ruby为例.在app目录下添加一个text文件.命名为app.rb

到了这一步就可以开始写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 => UserControl, :name => "app"}.merge(options)
    Application
.current.load_root_visual(options[:type].new, "#{options[:name]}.xaml")
  end
 
  def root
    application
.root_visual
  end
 
  def method_missing(m)
    root
.send(m)
  end
end

class FrameworkElement
  def method_missing(m)
    find_name(m
.to_s.to_clr_string)
  end
end

class App 
< SilverlightApplication
  use_xaml

  def initialize
    txtMessage
.text = "Welcome to Ruby in Silverlight"
  end
end

App
.new


为了运行起来需要在vs中设置一下.
在网站属性页中的启动选项选择"启动外部程序",选中sdk中的"Chiron.exe".命令行参数为"/b".工作目录设置为项目所在目录.

按F5运行程序

浏览器打开http://localhost:2060.在这里你可以用目录浏览的方式查看文件.
点击default.htm

显示了"Welcome to Ruby in Silverlight".这是由rb文件控制的.
修改代码.
  def initialize
    
#txtMessage.text = "Welcome to Ruby in Silverlight"
  end

注释掉设置文本的语句.
按F5刷新浏览器

显示"Hello World".这是xaml自己描述的.

ok  介绍完毕.

https://files.cnblogs.com/nasa/silverlight/start.zip


posted @ 2008-04-02 13:52  王喆(nasa)  阅读(3237)  评论(7编辑  收藏  举报