silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了《SilverLight2快速入门》之基本控件Button,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
前面我们搭建了开发环境,并且创建了一个基本的SilverLight应用程序。本节我们开始研究界面控件的用法。 注意: 做SilverLight有一点需要记住,这是运行在客户端宿主环境中的,所以这里的控件不是服务器控件。换句话说,SilverLight的运行需要客户端安装.NET Framework 3,
虽然宿主环境是浏览器,但是程序是下载到本地运行的,这和WPF机理一致,毕竟SilverLight
前面我们搭建了开发环境,并且创建了
一个基本的SilverLight应用程序。本节我们开始研究界面控件的
用法。
注意:
做SilverLight有一点需要记住,这是运行在客户端宿主环境中的,所以这里的控件不是服务器控件。换句话说,SilverLight的运行需要客户端安装.NET Framework 3,
虽然宿主环境是浏览器,但是程序是下载到本地运行的,这和WPF机理一致,毕竟SilverLight代号是WPF/E。
我们所用到的标准控件都来自
System.Windows.Controls 命名空间,具体成员说明可以查阅SDK。
Button控件绝对是最常用的控件。所以第
一个讲解。其实我们在
Hello程序里见过了。我们举个例子说明Button的声明和事件绑定,顺
便通过演示来理解控件
在本地运行的机理。
<
UserControl
x:Class
="_51CTO.lesson02.button"
xmlns
="http://scheR_319_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_319_11845@as.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控件写法基本一致。
namespace _51CTO.lesson02
{
public partial
class Button : UserControl
{
private
int times
=0;
public Button()
{
initializeComponent(
);
}
private
void Button_Click(
object sender,RoutedEventArgs
E)
{
times++;
message
Box.Show(time
s.To
String(),
"提示",
message
BoxButton
.oK
);
}
}
}
这是SilverLight XAML C#
代码,实现计数器累加和
显示。与ASP.NET不同,我们已不需要
考虑计数器状态保存的问题了。
运行结果如下:
这是个很
简单的例子。说明Button控件的基本设置和Click事件处理,当然Button的
属性和事件不止如此,笔者抛砖引玉,更多详细@L_
607_12@见SDK。
上面的控件使用
用法没有新意,与传统控件不一样的是,Button属于控件@L_
607_12@模型,关于“控件@L_
607_12@模型”我们会在后期单独讲解。先看
一个在Button上
显示其它图形的做法:
<
UserControl
x:Class
="_51CTO.lesson02.buttonImage"
xmlns
="http://scheR_319_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_319_11845@as.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://scheR_319_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_319_11845@as.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
>
namespace _51CTO.lesson02
{
public partial
class ButtonContent : UserControl
{
public ButtonContent()
{
initializeComponent(
);
}
private
void Slider_ValueChanged(
object sender,RoutedPropertyChangedEventArgs<
double>
E)
{
textBlock1.Text = ((
int)Slider1.
value).To
String(
);
}
}
}
有兴趣的朋友可以发挥想象力将Button玩出更强大的
效果来。
大佬总结
以上是大佬教程为你收集整理的《SilverLight2快速入门》之基本控件Button全部内容,希望文章能够帮你解决《SilverLight2快速入门》之基本控件Button所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。