Silverlight学习笔记:第一章:Silverlight3.0开发工具和开发实践

 一、 开发工具

1. Visual Studio 2008SP1:

2. Silverlight Tools:

---Silverlight运行时,是开发和运行Silverlight应用程序的必需品。

3. Expression Blend 3

---一个所见即所得的Silverlight和WPF程序设计和开发的工具,他是微软Expressio Studio系列产品中的一员。

二、第一个小程序:

1. 创建一个解决方案,这个解决方案包括一个ASP.NET网站项目和一个Silverlight应用程序项目。

 1)ASP.net项目

-------------Default.aspx:ASP.net默认页面。

-------------TestPage.aspx和TestPage.html:都是用来承载Silverlight应用程序的测试页。

-------------Silverlight.js:支持HTML一类页面中运行Silverlight。

  2)Silverlight应用程序

新建silverlight项目会自动新增两个XAML文件,分别是APP.XAML和MainPage.XAML两个文件。APP.xaml主要是用于控制整个Silverlight应用程序的加载入口和异常的处理;

关于APP.xaml页面

代码1(APP.xaml):

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class
="Silverlight应用程序.App"
>
<Application.Resources>

</Application.Resources>
</Application>

代码2(APP.xaml.cs):

public partial class App : Application
{

public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;

InitializeComponent();
}

private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}

private void Application_Exit(object sender, EventArgs e)
{

}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// 如果应用程序是在调试器外运行的,则使用浏览器的
// 异常机制报告该异常。在 IE 上,将在状态栏中用一个
// 黄色警报图标来显示该异常,而 Firefox 则会显示一个脚本错误。
if (!System.Diagnostics.Debugger.IsAttached)
{

// 注意: 这使应用程序可以在已引发异常但尚未处理该异常的情况下
// 继续运行。
// 对于生产应用程序,此错误处理应替换为向网站报告错误
// 并停止应用程序。
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}

关于MainPage.xaml页面(第一个silverlight应用程序--渐变文本)

前台代码:

<UserControl x:Class="Silverlight应用程序.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:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<TextBlock Text="测试Silverlight" FontFamily="Arial" FontSize="50">
<TextBlock.Foreground>
<!-- 使用渐变画刷填充文本产生渐变效果-->
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<!-- 生命渐变色分别为黑色和白色-->
<GradientStop Color="Black"></GradientStop>
<GradientStop Color="White" Offset="1"></GradientStop>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</Grid>
</UserControl>

运行结果:

--------------- :<UserControl>是MainPage.xaml的root元素,Grid是默认布局元素。 MainPage.xaml是Silverlight应用程序的默认用户控件,编译运行时会自动加载该用户控件到托管的web页面中运行,它类似ASP.net中的Default.aspx页面,在silverlight3.0中,MainPage.xaml是以用户控件“UserControl”作为根元素的形式加载的,所以可以把MainPage.xaml理解为一个MainPage的用户控件,他是silverlight运行时默认启动的xaml用户控件,MainPage类继承UserControl,而UserControl类是所有xaml文件的基类。

三、使用silverlight控件实现交互:

1. 前台代码:

<Canvas Background="White">
<TextBox x:Name="tb_input"
Width
="150"
Height
="50"
Canvas.Top
="30"
Canvas.Left
="30"
FontSize
="25">
</TextBox>
<Button x:Name="Btn1"
Width
="120"
Height
="50"
Canvas.Top
="30"
Canvas.Left
="200"
FontSize
="20"
Content
="请单击我"
Click
="Btn1_Click">
</Button>
<TextBlock x:Name="tbl"
Canvas.Top
="130"
Canvas.Left
="40"
Foreground
="YellowGreen"
FontSize
="50"></TextBlock>
</Canvas>

2. 后台代码:

 private void Btn1_Click(object sender, RoutedEventArgs e)
{
tbl.Text = tb_input.Text;
}

3. 运行结果:


四、使用Blend 3 开发:

1. 可以使用VS2008创建或打开一个silverlight项目,然后再切换到Blend 3中进行编辑,也就是VS2008可以和Blend3共享一个silverlight文件。

  

 

posted on 2011-10-07 00:04  我is小黑  阅读(705)  评论(0编辑  收藏  举报

导航