概述

Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。

本文为系列文章第10篇,主要介绍Silverlight 2中的用户控件使用。

创建用户控件

在Silverlight 2中,我们可以根据开发自定义控件或者创建用户控件,以达到控件重用的目的,添加一个新的用户控件:

编写用户控件实现代码:

<Grid x:Name="LayoutRoot" Background="White">
<Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Opacity="0.7" Fill="#FF8A8A8A"/>
<Border CornerRadius="15" Width="400" Height="150" Background="LightPink" Opacity="0.9">
<StackPanel Orientation="Horizontal" Height="50">
<Image Source="info.png" Margin="10 0 0 0"></Image>
<Button Background="Red" Width="120" Height="40"
Content="OK" Margin="10 0 0 0" FontSize="18"/>
<Button Background="Red" Width="120" Height="40"
Content="Cancel" Margin="50 0 0 0" FontSize="18"/>
</StackPanel>
</Border>
</Grid>

在需要使用该用户控件的页面XAML中注册命名空间:

使用用户控件:

<Grid x:Name="LayoutRoot" Background="#46461F">
<uc:ConfirmBox x:Name="mybox"></uc:ConfirmBox>
</Grid>

整个过程就这么简单,运行后效果如下:

为用户控件添加属性

简单的修改一下上面示例中的XAML文件,添加一个文本块控件,用它来显示文字提示信息。

<Grid x:Name="LayoutRoot" Background="White">
<Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Opacity="0.7" Fill="#FF8A8A8A"/>
<Border CornerRadius="15" Width="400" Height="150" Background="LightPink" Opacity="0.9">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="90"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="message" FontSize="18" Foreground="White"
HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="50 20 0 0"/>
<StackPanel Orientation="Horizontal" Height="50" Grid.Row="1">
<Image Source="info.png" Margin="10 0 0 0"></Image>
<Button Background="Red" Width="120" Height="40"
Content="OK" Margin="10 0 0 0" FontSize="18"/>
<Button Background="Red" Width="120" Height="40"
Content="Cancel" Margin="50 0 0 0" FontSize="18"/>
</StackPanel>
</Grid>
</Border>
</Grid>

定义属性:

public partial class ConfirmBox : UserControl
{
public ConfirmBox()
{
InitializeComponent();
} public String Message
{
get { return this.message.Text; }
set { this.message.Text = value; }
}
}

在页面使用用户控件的属性,XAML编辑器能够识别出属性并提示:

为ConfirmBox控件的Message属性赋值:

<Grid x:Name="LayoutRoot" Background="#46461F">
<uc:ConfirmBox x:Name="mybox" Message="使用用户控件成功"></uc:ConfirmBox>
</Grid>

运行后效果如下所示:

动态添加用户控件

用户控件可以动态的添加到页面中,修改一下Page.xaml中的XAML代码,放入一个Canvas作为用户控件的容器。

<Grid x:Name="LayoutRoot" Background="#46461F">
<Canvas x:Name="ContainerCanvas"> </Canvas>
</Grid>

编写添加用户控件代码:

private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
ConfirmBox confirmbox = new ConfirmBox(); confirmbox.Message = "动态添加用户控件成功!"; ContainerCanvas.Children.Add(confirmbox);
}

运行后效果如下所示,当然我们也可以控制用户控件显示的位置等。

结束语

本文简单介绍了在Silverlight 2中使用用户控件,包括创建用户控件、添加属性、动态添加用户控件等内容,你可以从这里下载本文示例代码。

下一篇:一步一步学Silverlight 2系列(11):数据绑定

作者:TerryLee
出处:http://terrylee.cnblogs.com 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

一步一步学Silverlight 2系列(10):使用用户控件的更多相关文章

  1. 一步一步学Silverlight 2系列文章

    概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...

  2. 一步一步学Silverlight 2系列(32):图形图像综合实例—“功夫之王”剧照播放

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  3. 一步一步学Silverlight 2系列(31):图形图像综合实例—实现水中倒影效果

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  4. 一步一步学Silverlight 2系列(30):使用Transform实现更炫的效果(下)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. 一步一步学Silverlight 2系列(29):使用Transform实现更炫的效果(上)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. 一步一步学Silverlight 2系列(28):图片处理

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  7. 一步一步学Silverlight 2系列(27):使用Brush进行填充

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  8. 一步一步学Silverlight 2系列(26):基本图形

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  9. 一步一步学Silverlight 2系列(25):综合实例之Live Search

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  10. 一步一步学Silverlight 2系列(24):与浏览器交互相关辅助方法

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

随机推荐

  1. 【Tyvj1982】武器分配(费用流)

    题意:有N个人要从A个物品中各取一个,B个物品中各取一个,选取第i个A类物品和第j个B类物品的费用是(a[i]-b[j])^2 求最小总花费 n<=a,b<=80 a[i],b[i]< ...

  2. PHP输出控制函数(ob系列函数)

    PHP输出控制函数(ob系列函数) flush — 刷新输出缓冲ob_clean — 清空(擦掉)输出缓冲区ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲ob_end_flush — ...

  3. golang并发编程goroutine+channel(一)

    go语言的设计初衷除了在不影响程序性能的情况下减少复杂度,另一个目的是在当今互联网大量运算下,如何让程序的并发性能和代码可读性达到极致.go语言的并发关键词 "go" go dos ...

  4. Wannafly挑战赛11 D 题 字符串hash + 卡常

    题目链接 https://ac.nowcoder.com/acm/contest/73#question map与order_map https://blog.csdn.net/BillCYJ/art ...

  5. Kafka windows下的安装

    1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载JDK1.2 安装完成后需 ...

  6. Maven打包时过滤测试代码或指定特定的测试类(maven-surefire-plugin)

    1.过滤整个测试代码,可以直接在命令行上指定 mvn clean install -Dmaven.test.skip=true 提示:以上为举例,具体的构建阶段可以自定义,其中maven.test.s ...

  7. Invocation of destroy method failed on bean with name ‘XXXX’

    项目启动报错问题:Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.spri ...

  8. [React] Create and import React components with Markdown using MDXC

    In this lesson I demonstrate how to use the library MDXC to create and import React components with ...

  9. Intel Edision —— 从SSH无法连接到systemd

    前言 原创文章,转载引用务必注明链接.如有疏漏,欢迎斧正. 最近在试用Wyliodrin,安装过程中出现了两个问题,一是无法使用SSH登录到Edison:二是EDISON磁盘的问题.分别涉及到syst ...

  10. 生成和解析txt文件

    package txt; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; imp ...