《SilverLight2快速入门》之Hello SilverLight
时间 2020-10-06
标签
编程
浏览器
ide
布局
测试
this
spa
设计
xml
blog
环境搭建好了,下面从一个最简单的应用程序开始练习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文件,这是咱们接下来不断重复的步骤。