《SilverLight2快速入门》之Hello SilverLight

环境搭建好了,下面从一个最简单的应用程序开始练习SilverLight编程。
打开VisualStudio2008,添加新项目(笔者习惯C#编程):
��寤哄��ㄧ�搴�
 
在解决方案浏览器中默认会有App.xaml和Page.xaml,App.xaml是启动入口,Page.xaml就是我们的一个SilverLight程序。你可以在项目中创建多个xaml文件
具体如图:
 
 
解决方案浏览器:
这里我们用编写代码的方式设计界面,效果如下
Page.xaml代码如下:
< UserControl x:Class ="SilverlightApplication1.Page"
         xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
         xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"    
         Width ="400" Height ="300" >
         < Grid x:Name ="LayoutRoot" Background ="White" >
                 < Button x:Name ="myButton" Content ="点我" Width ="200" Height ="50"
                 Foreground ="Red" Click ="myButton_Click" > </ Button >
         </ Grid >
</ UserControl >
< UserControl> 是SilverLight的根容器
< Grid >是布局容器,如果有Java Swing编程经验的人,对这个理解会比较快一点。
< Button >是一个按钮
 
Page.xaml.cs代码如下:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SilverlightApplication1
{
         public partial class Page : UserControl
        {
                 public Page()
                {
                        InitializeComponent();
                }

                 private void myButton_Click( object sender, RoutedEventArgs e)
                {
                         this.myButton.Content = "Hello SilverLight!";
                         this.myButton.Background = new SolidColorBrush(Colors.Red);
                }
        }
}
实现如果点击按钮,则在按钮上显示 "Hello SilverLight!", 并且按钮背景色变为红色。
运行测试和其它应用程序一致,点击运行图表或者菜单项就可以了。
运行效果如下:
OK,从创建项目到创建xaml文件,这是我们接下来不断重复的步骤。
 

你可能感兴趣的:(职场,silverlight,休闲,Silverlight2)