教你使用Visual Studio 2010创建简单的Silverlight应用程序2014-12-08Silverlight是创建动态的引人的RIAs(Rich Internet Application)的新方法。这里教你创建简单的Silverlight应用程序。1. 打开VS,新建项目,选择Silverlight应用程序模板。2. 命名mySimpleSilverlightApplication,点击确定。3. 在弹出的对话框中,清除“在新网站中承载Silverlight应用程序”复选框,点击确定。

4. 你的界面应该是这样的。

5. 拖四个控件(2个标签、1个文本框和1个按钮)。

6. x:Name属性代表了Silverlight控件的名称(或ID)。确保最后的代码是这样的:
<UserControl x:Class="mySimpleSilverlightApplication.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"d:DesignHeight="204" d:DesignWidth="400" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"><Grid x:Name="LayoutRoot" Background="White" Height="186"><dataInput:Label Content="My First Silverlight App" Height="21"HorizontalAlignment="Left" Margin="42,32,0,0" Name="lblTitle"VerticalAlignment="Top" Width="225" FontWeight="Bold"/><Button Click="btnName_Click" Content="Greeting"Height="23" HorizontalAlignment="Left"Margin="42,115,0,0" Name="btnName" VerticalAlignment="Top"Width="75"/><TextBox Height="23" HorizontalAlignment="Left"Margin="104,72,0,0" Name="txtbxName" VerticalAlignment="Top"Width="163"/><dataInput:Label Content="Name:" Height="21"HorizontalAlignment="Left" Margin="42,72,0,0" Name="lblName"VerticalAlignment="Top" Width="56"/></Grid></UserControl>
界面是这样的:

7. 右击MainPage.xaml查看代码。8. 添加代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace mySimpleSilverlightApplication{public partial class MainPage : UserControl{public MainPage(){InitializeComponent();}private void btnName_Click(object sender,RoutedEventArgs e){string myNamePrefix = "Length of Name: ";string myName = txtbxName.Text;int myNameLength = 0;myNameLength = myName.Length;MessageBox.Show(myNamePrefix +myNameLength.ToString());btnName.Content = "Goodbye";}}}
9. F5测试。在SharePoint站点上添加Silverlight Web部件。

作者:csdn博客 张世辉