Silverlight2 + VS2008支持C#的SilverLightHelloworld教程(转)
来源:互联网 发布:淘宝店铺活动 编辑:程序博客网 时间:2024/06/14 08:47
转自http://blog.csdn.net/wangweixing2000/archive/2008/04/16/2297514.aspx
今天装好Silverlight后,第一个程序就是写个helloworld,这里贡献给Silverlight的初学者,希望对你有用!
这里我认为您已经装好VS2008和Siverlight2 beta1,:)
下面就开始我们的helloworld之旅吧!hoho
1、打开vs2008-〉创建Siverlight Application项目
2、输入SilverLightHelloWorld->OK我们将看到:
3、选择第二项Generate an .... ->OK,我们将看到下图创建好的工程:
4、我们试着编译以下F7:
编译通过没有问题,哈哈
我们将看到page.xaml代码:
<UserControl x:Class="SilverlightHelloWorld.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
5、我们需要更改其xaml代码,更改Grid的背景色并添加一个Button,如下代码:
<UserControl x:Class="SilverlightHelloWorld.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Red">
<Button Content="HelloWorld" Width="100" Height="20">
</Button>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Red">
<Button Content="HelloWorld" Width="100" Height="20">
</Button>
</Grid>
</UserControl>
背景改成了红色,添加了HelloWorld按钮,效果如下图:
6、我们再给HelloWorld加个Click事件,代码入下:
<UserControl x:Class="SilverlightHelloWorld.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Red">
<Button Content="HelloWorld" Width="100" Height="20" Click="Button_Click">
</Button>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Red">
<Button Content="HelloWorld" Width="100" Height="20" Click="Button_Click">
</Button>
</Grid>
</UserControl>
还需要添加事件的处理,在Page.xaml.cs中,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
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 SilverlightHelloWorld
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//这里添加代码
SolidColorBrush brush = (SolidColorBrush)this.LayoutRoot.Background;
if (brush.Color == Colors.Red)
{
this.LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
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 SilverlightHelloWorld
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//这里添加代码
SolidColorBrush brush = (SolidColorBrush)this.LayoutRoot.Background;
if (brush.Color == Colors.Red)
{
this.LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
}
}
}
}
代码逻辑很简单,每点击一次按钮会更改一次背景色。
F5运行看看效果:
点击后的效果:
XAML何以玩出很多花样,剩下的就看你了,hoho!
作者:王卫星
转载注明出处,谢谢!
- Silverlight2 + VS2008支持C#的SilverLightHelloworld教程
- Silverlight2 + VS2008支持C#的SilverLightHelloworld教程(转)
- Silverlight2 Beta2 最新中文版下载(支持VS2008中文版)
- 推荐一个优秀的Silverlight2教程
- 给vs2008安装silverlight2开发工具的‘曲折’过程
- Silverlight2.0 VS2008 安装环境
- Silverlight2应用程序的部署
- silverlight2
- Silverlight2.0 将直接支持中文
- Silverlight2 Beta2的小例子
- C# vs2008安装部署工程制作教程
- vs2008 提示未安装c#的visual studio语言支持 。。。解决办法
- silverlight2 beta1转beta2经验
- 在VS2008下搭建Silverlight2.0开发环境
- C# VS2008转VS2005
- 支持微软SilverLight2 开源项目Eclipse4sl发布
- 让vs2005支持vs2008的sln工程
- MIX08,迎来Silverlight2的新时代
- 在mobile数据库下如何实现分页查询数据的功能
- 鼠标划过某个窗口或者控件,监控他
- 使用动态重写,,实现JS的d单件模式(singleton)
- ASP.NET中常用的文件上传下载方法 (转)
- 诗词收藏与鉴赏
- Silverlight2 + VS2008支持C#的SilverLightHelloworld教程(转)
- 小便看性格
- ARM汇编语言学习笔记之一(ARM简介)
- 小倪的博客开业了
- Customer类定义
- 历史上著名的十大超级愚蠢事件
- 企业4+1核心竞争力模型
- 一些常用的辅助代码 (网络收藏)
- C++ STL algorithm部分用法(1)