注意:
做SilverLight有一点需要记住,这是运行在客户端宿主环境中的,所以这里的控件不是服务器控件。换句话说,SilverLight的运行需要客户端安装.NET Framework 3,虽然宿主环境是浏览器,但是程序是下载到本地运行的,这和WPF机理一致,毕竟SilverLight代号是WPF/E。
我们所用到的标准控件都来自
System.Windows.Controls 命名空间,具体成员说明可以查阅SDK。
Button控件绝对是最常用的控件。所以第一个讲解。其实我们在Hello程序里见过了。我们举个例子说明Button的声明和事件绑定,顺便通过演示来理解控件在本地运行的机理。
<
UserControl
x:Class
="_51CTO.lesson02.Button"
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 Name ="button1" Width ="200" Height ="100" Content ="这是一个按钮" Click ="Button_Click" > </ Button >
</ Grid >
</ UserControl >
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 Name ="button1" Width ="200" Height ="100" Content ="这是一个按钮" Click ="Button_Click" > </ Button >
</ Grid >
</ UserControl >
上面声明了一个Button,语法与ASP.NET控件写法基本一致。
运行结果如下:
<
UserControl
x:Class
="_51CTO.lesson02.ButtonImage"
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 Height ="100" Width ="300" HorizontalContentAlignment ="Left" >
< Grid >
< Grid.ColumnDeFinitions >
< ColumnDeFinition />
< ColumnDeFinition />
</ Grid.ColumnDeFinitions >
< Grid.RowDeFinitions >
< RowDeFinition />
</ Grid.RowDeFinitions >
< Image Source ="Naruto.jpg" Grid.Column ="0" Grid.Row ="0" > </ Image >
< TextBlock Grid.Column ="1" Grid.Row ="0" VerticalAlignment ="Center" FontSize ="20" >图片按钮 </ TextBlock >
</ Grid >
</ Button >
</ Grid >
</ UserControl >
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 Height ="100" Width ="300" HorizontalContentAlignment ="Left" >
< Grid >
< Grid.ColumnDeFinitions >
< ColumnDeFinition />
< ColumnDeFinition />
</ Grid.ColumnDeFinitions >
< Grid.RowDeFinitions >
< RowDeFinition />
</ Grid.RowDeFinitions >
< Image Source ="Naruto.jpg" Grid.Column ="0" Grid.Row ="0" > </ Image >
< TextBlock Grid.Column ="1" Grid.Row ="0" VerticalAlignment ="Center" FontSize ="20" >图片按钮 </ TextBlock >
</ Grid >
</ Button >
</ Grid >
</ UserControl >
这里用到了Grid布局的知识,简单的实现左右布局,我们会在后期有专门的布局学习。其实就是如下一样的按钮:
你甚至可以在Button上加其它输入控件,简直就是个容器一样,比如下面代码:
<
UserControl
x:Class
="_51CTO.lesson02.ButtonContent"
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 Name ="button1" Width ="200" Height ="100" >
< StackPanel >
< Ellipse Height ="40" Width ="40" Fill ="Blue" />
< Slider Name ="Slider1" Width ="100" ValueChanged ="Slider_ValueChanged" > </ Slider >
< TextBlock Name ="TextBlock1" TextAlignment ="Center" >按钮 </ TextBlock >
</ StackPanel >
</ Button >
</ Grid >
</ UserControl >
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 Name ="button1" Width ="200" Height ="100" >
< StackPanel >
< Ellipse Height ="40" Width ="40" Fill ="Blue" />
< Slider Name ="Slider1" Width ="100" ValueChanged ="Slider_ValueChanged" > </ Slider >
< TextBlock Name ="TextBlock1" TextAlignment ="Center" >按钮 </ TextBlock >
</ StackPanel >
</ Button >
</ Grid >
</ UserControl >
C#代码如下:
有兴趣的朋友可以发挥想象力将Button玩出更强大的效果来。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。