创建和使用/重用用户控件在Silverlight
这里的场景,需要在Silverlight中搜索文本框,可能会被用于在不同的页面,甚至其他项目本身。
为了实现这样一个目标,您将创建Silverlight应用程序,并将它命名为ReusingUserControl。你是不是要使用RIA服务,然后接受默认为这项工作。你的页面看起来像如下的东西,对不对?
{S0}
在Silverlight项目中添加一个新的文件夹命名CommonControls。然后,右键单击新文件夹,并创建一个新项目,选择Silverlight用户控制,并命名它UCSearch。你的屏幕看起来像如下的东西,对不对?
{S1}
对于这种控制,我们将使用具有图像水印和一个工具提示和最终的结果将如下所示:
{S2}
{S3}
为了工作的水痕,您将需要使用参考的System.Windows.Interactivity。此外,您将需要使用一个CSHARP类命名Watermark.cs,这是我从一些很好的开发在Web借用。您还需要有一个形象,为您在上面看到或类似,并把它添加到您的项目。下面的,最终的XAML。请注意的高度和宽度,以适应文本区域。<UserControl x:Class="SilverlightApplicationSideBar.CommonControls.UCSearch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Watermark"
xmlns:Interactivity<span class="code-keyword">=
"</span>clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
d:DesignHeight="32" d:DesignWidth="172">
<StackPanel Orientation="Horizontal">
<TextBox Height="23" Name="tbSearch" Width="160"
HorizontalAlignment="left" Margin="8,6,2,2"
VerticalAlignment="Top"
TextWrapping="Wrap" >
<Interactivity:Interaction.Behaviors>
<local:Watermark Text="Search Area" Foreground="LightGray" />
</Interactivity:Interaction.Behaviors>
<ToolTipService.ToolTip>
<ToolTip >
<Image Source="/ ReusingUserControl;component/Images/zoomtxt.jpg">
</Image>
</ToolTip>
</ToolTipService.ToolTip>
</TextBox>
</StackPanel>
</UserControl>
注意:引用的名称空间System.Windows.Interactivity。"它的水痕。
项目zoomtxt.jpg形象的参考提示服务。
现在,后面的代码怎么样?
在你后面的代码将KeyUp事件,在那里你会捕获的时间,当用户按下键盘上的Enter。当用户按ENTER键,代码将检查是否有任何的文字,如果有,那么代码将设置公共财产文本,该文本的价值。后来,使用您的控制页面,将读取公共财产。现在,看到背后下面的代码:{C}
记住,你将需要Watermark.cs在编译您的项目。
到目前为止好,你准备好您的用户控制在Silverlight项目的其他部分使用和重用。
要做到这一点,你将需要宣布用户控制。由于在这个例子中,它是在同一项目中,你并不需要申报的大会,但根本的命名空间如下: xmlns:uc="clr-namespace:ReusingUserControl.CommonControls"
接下来,请您对您的新的一页如下的用户控制的参考: <uc:UCSearch Grid.Row="2" Margin="0,15,0,0" x:Name="txtSearch"
KeyUp="txtSearchArea_KeyUp"></uc:UCSearch>
整个XAML的主要页面显示如下:<UserControl x:Class="ReusingUserControl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:ReusingUserControl.CommonControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<uc:UCSearch Grid.Row="2" Margin="0,15,0,0" x:Name="txtSearch"
KeyUp="txtSearchArea_KeyUp"></uc:UCSearch>
</Grid>
</UserControl>
如果你在看代码,你会看到,我到一个正在通过的KeyUp触发事件的参考,权呢?
好了,后面的代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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;
using System.Windows.Navigation;
namespace SilverlightApplicationSideBar
{
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
}
//<span class="code-comment"> Executes when the user navigates to this page.
</span> protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
protected void txtSearchArea_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (txtSearch.Text != null && txtSearch.Text != "")
{
MessageBox.Show(txtSearch.Text);
}
}
}
}
}
公告"上的KeyUp,我检查的KeyUp是完成时的时间。当做到这一点,其实我读的文字命名,我刚才说我UCSearch控制的公共财产。 BOOMMMM!你现在可以在文本框的值。
什么是有关在这个简单的演示?在您的应用程序的许多领域,水印,工具提示和内置的用户控件的创建可重复使用。这里的概念是让输入的文本,并通过数据库来过滤你的项目是返回的数据。
注:为了节省时间和提高生产力,这听起来像,建立可重用的用户控制是一个很好的路要走。你觉得呢?
我,包括源代码,这样你就可以启动。
享受!
Marcio科埃略