返回首页


创建和使用/重用用户控件在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科埃略

回答

评论会员:Saty99 时间:2012/01/27
感谢您张贴的文章

当我在XAML中指定Text属性的值在MainPage.xaml中的用户控制和尝试运行它抛出一个错误"类型"System.StackOverflowException"ReusingUserControl发生未处理的异常"不知道为什么它是引发此错误。是否有任何变通assing文本propert德兴时间。

谢谢你
沙耶
评论会员:Marcio_Coelho 时间:2012/01/27
您好,我为原型创建和专门阅读是什么类型的加载控制后的。
基本上,在用户控件中创建的财产需要一个依赖项属性所取代。基本上,虽然我们在经典的C添加属性使用get和set#,Silverlight中,我们需要使用的依赖关系的财产。

因此,请从下面的用户控件的代码替换:
public partial class UCSearch : UserControl

    {

        public UCSearch()

        {

            InitializeComponent();

 

        }

 

        protected void txtSearchArea_KeyUp(object sender, KeyEventArgs e)

        {

            if (e.Key == Key.Enter)

                {

                if (tbSearch.Text != null && tbSearch.Text != "")

                {

                    Text = tbSearch.Text;

                }

                else

                {

                   Text = "";

                }

            }

        }

 

        //From this point until the end is code for the dependency

        public string Text

        {

            get {   return (string)GetValue(UCSearchProperty); }

            set

            {

                SetValue(UCSearchProperty, value);

            }

        }

 

        public static readonly DependencyProperty UCSearchProperty = DependencyProperty.Register("Text", typeof(string), typeof(UCSearch),

                    new PropertyMetadata(new PropertyChangedCallback(TextChanged)));

 

        private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            var c = d as UCSearch;

            if (c != null)

            {

                c.Text = e.NewValue as string;

                c.tbSearch.Text = c.Text;

            }

        }

做到这一点后,你可以设置Text属性的用户控件的页面,您使用的控制如下:

<uc:UCSearch Grid.Row="1" Margin="0,15,0,0" x:Name="txtSearch" Text="Saya"

                    KeyUp="txtSearchArea_KeyUp"></uc:UCSearch>
我编译它,它工​​作得很好。如果你有问题,给我发电子邮件,我会打出一个更干净的代码为例。

最好的愿望
Marcio

修改09月30日'11
沃尔特Kuong
评论会员:游客 时间:2012/01/27
感谢你这篇文章!你能展示一个例子,在其他项目中使用这个控制吗?此外,你可以显示一个例子,如何引用一个用户控件?在空间首页的UI元素!