最小的Silverlight示例

时间:2009-01-17 17:08:35

标签: c# silverlight boo

Silverlight应用程序的最小例子是什么?

例如,我使用IronPython制作了以下示例:

from System.Windows import Application
from System.Windows.Controls import Canvas, TextBlock

canvas = Canvas()
textblock = TextBlock()
textblock.FontSize = 24
textblock.Text = 'Hello!'
canvas.Children.Add(textblock)

Application.Current.RootVisual = canvas

然后我使用 chiron 并创建了一个.xap文件。没有(显式)XAML,没有任何东西。是否有可能在C#中做同样的事情?我可以从命令行编译一个源文件?如果是这样,源代码是什么?

我的动机是尝试使用非常规语言创建silverlight应用程序,现在我被困在Boo ......

2 个答案:

答案 0 :(得分:2)

using System;
using System.Windows;
using System.Windows.Controls;

namespace MimimalSilverlightApp
{
    public class App : Application
    {
        public App()
        {
            this.Startup += this.Application_Startup;
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var canvas = new Canvas();

            var textblock = new TextBlock();
            textblock.FontSize = 24;
            textblock.Text = "Hello!";
            canvas.Children.Add(textblock);

            this.RootVisual = canvas;
        }
    }
}

答案 1 :(得分:2)

找到一个F#示例here

基于它制作了一个Boo示例。源代码:

namespace Hello
import System
import System.Windows
import System.Windows.Controls

class MyPage(UserControl):
    def constructor():
        canvas = Canvas()
        textblock = TextBlock()
        textblock.FontSize = 24
        textblock.Text = "Hello!"
        canvas.Children.Add(textblock)

        self.Content = canvas

class MyApp(Application):

    def constructor():      
        Startup += onStartup


    def onStartup(sender, e):

        self.RootVisual = MyPage()

内置:

booc -nostdlib -target:library -lib:"C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies","C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Client" -r:System.Windows.dll -r:System.Windows.Controls.dll Hello.boo

AppManifest.xaml:

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            EntryPointAssembly="Hello" 
            EntryPointType="Hello.MyApp" RuntimeVersion="2.0.31005.0">
  <Deployment.Parts>
    <AssemblyPart x:Name="Hello" Source="Hello.dll" />
    <AssemblyPart x:Name="Boo.Lang" Source="Boo.Lang.dll" />
  </Deployment.Parts>
</Deployment>

然后创建了一个普通的.zip文件,其中包含AppManifest.xaml,Hello.dll和Boo.Lang.dll(其目录中的一个Boo库)并将其重命名为hello.xap。

要查看它的HTML:

<html>
<head></head>
<body>
<object id="SilverlightPlugin" data="data:application/x-silverlight,"
 type="application/x-silverlight-2" width="450" height="540">
  <param name="source" value="hello.xap"/>

  <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
    <img src="http://go.microsoft.com/fwlink/?LinkId=108181"
     alt="Get Microsoft Silverlight" style="border-style: none"/>
  </a>
</object>

</body>
</html>

生成的hello.xap是43 KB,比使用IronPython和DLR获得的1.5 MB要好得多。