微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

《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文件,这是我们接下来不断重复的步骤。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐