Silverlight2 + VS2008支持C#的SilverLightHelloworld教程
2008-04-16 16:33
399 查看
今天装好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>
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>
背景改成了红色,添加了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>
还需要添加事件的处理,在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);
}
}
}
}
代码逻辑很简单,每点击一次按钮会更改一次背景色。
F5运行看看效果:
点击后的效果:
XAML何以玩出很多花样,剩下的就看你了,hoho!
作者:王卫星
转载注明出处,谢谢!
这里我认为您已经装好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>
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>
背景改成了红色,添加了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>
还需要添加事件的处理,在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);
}
}
}
}
代码逻辑很简单,每点击一次按钮会更改一次背景色。
F5运行看看效果:
点击后的效果:
XAML何以玩出很多花样,剩下的就看你了,hoho!
作者:王卫星
转载注明出处,谢谢!
相关文章推荐
- Silverlight2 + VS2008支持C#的SilverLightHelloworld教程(转)
- C# windows application Hello World
- [连载]C#程序设计(03)--- C#核心编程-1 --- Hello, world!
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四十五)制作精美的可任意拖放对象的物品栏及装备栏
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四十五)制作精美的可任意拖放对象的物品栏及装备栏
- MiniUI快速入门教程(二)编写第一个MiniUI程序:Hello, world!
- automake hello world 使用教程 例子
- C# Hello World
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十四) Be careful!前方怪物出没
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十七)战斗前夜之构建动态障碍物系统
- C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(四)实现2D人物动画①
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十五) 精灵控件横空出世!②
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十八) 完美精灵之八面玲珑(WPF Only)②
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十二)重构 – 让代码插上翅膀自由飞翔
- 在VS2008中使用C#代码打印 RichTextBox 控件的内容[方法采自微软支持][Part 2]
- Direct2D教程(二)来看D2D世界中的Hello,World
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):目录
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(八) 完美实现A*寻径动态动画
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四十六)飞行模式 – 自由VS天空
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(三十三) 锦上添花之魔法特效装饰