SilverLight学习笔记--建立Silverlight自定义控件(1)--外观设计
?? Silverlight 2 以豐富且強(qiáng)大可靠的控件模型聞名,該模型是平臺(tái)中包括的控件和第三方控件包的基礎(chǔ)。您也可以使用此控件模型構(gòu)建自己的控件。
? 在了解如何為新平臺(tái)編寫(xiě)自定義控件時(shí),我經(jīng)常先復(fù)制一些內(nèi)置控件:按鈕和列表框等等。這些控件可能表面看起來(lái)簡(jiǎn)單,但他們總是揭示了控件模型的關(guān)鍵功能并可以測(cè)試人們對(duì)這些功能的掌握程度。??
? 下面我們一起來(lái)一步步建立一個(gè)自定義控件MySilverButton.
1、打開(kāi)VS2008,文件-新建項(xiàng)目-Silverlight類(lèi)庫(kù),項(xiàng)目名輸入為 MyDesignButton。創(chuàng)建的Silverlight類(lèi)庫(kù)中默認(rèn)會(huì)有一個(gè)Class1.cs,這是一個(gè)普通的C#類(lèi),與Silverlight并無(wú)關(guān)系,可以選擇保留它利用VS的重構(gòu)功能換成喜歡的名字,也可以刪掉它再重新建立一個(gè)類(lèi)。總之我們的Silverlight類(lèi)庫(kù)中只需要保留一個(gè)我們要開(kāi)發(fā)的控件名字的類(lèi)就可以了。因此在此處,我們進(jìn)入后把Class1.cs改名為 MySilverButton.cs,讓此類(lèi)繼承自 ContentControl,代碼如下:
?
using?System;using?System.Net;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Documents;
using?System.Windows.Ink;
using?System.Windows.Input;
using?System.Windows.Media;
using?System.Windows.Media.Animation;
using?System.Windows.Shapes;
namespace?MyDesignButton
{
????public?class?MySilverButton:?ContentControl?
????{
????}
}
2、為此項(xiàng)目添加一個(gè)新文件夾,名稱(chēng)為themes,因?yàn)橄乱徊轿覀円诖宋募A中建立一個(gè)名為Generic.xaml的文件(默認(rèn)控件模板 ),用于存放我們自定義控件的外觀定義。建立方法如下:在解決方案管理器中選擇此項(xiàng)目,鼠標(biāo)右鍵彈出菜單,添加--新建項(xiàng),為此項(xiàng)目添加一個(gè)文件文件,默認(rèn)名稱(chēng)為T(mén)extFile1.txt,但我們需要把它的后綴名改為xaml,所以,其全名為 Generic.xaml。此文件內(nèi)容如下:
?
<ResourceDictionary???xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??xmlns:custom="clr-namespace:MyDesignButton">
????<Style?TargetType="custom:MySilverButton">
????????<Setter?Property="Template">
????????????<Setter.Value>
????????????????<ControlTemplate?TargetType="custom:MySilverButton">
????????????????????<Grid?x:Name="RootElement">
????????????????????????<Rectangle?x:Name="BodyElement"?Width="200"?Height="100"???Fill="Brown"??Stroke="Purple"?RadiusX="16"?RadiusY="16"?/>
????????????????????????<TextBlock?x:Name="ButtonCaption"??HorizontalAlignment="Center"??VerticalAlignment="Center"?FontSize="26"??/>
????????????????????</Grid>
????????????????</ControlTemplate>
????????????</Setter.Value>
????????</Setter>
????</Style>
</ResourceDictionary>
?
3、添加MySilverButton類(lèi)的構(gòu)造函數(shù),并在其構(gòu)造函數(shù)內(nèi)部加入代碼
?
this.DefaultStyleKey?=?typeof(MySilverButton);?
加入此代碼后,你才能在引用此控件時(shí)看到它的外觀。此時(shí)構(gòu)造函數(shù)如下
????????{
???????????????this.DefaultStyleKey?=?typeof(MySilverButton);
????????}
?
4、下面我們先來(lái)看看初步效果,為此我們需要另建一個(gè)項(xiàng)目,文件-新建項(xiàng)目-Silverlight應(yīng)用程序。項(xiàng)目名為:MySLbutton,項(xiàng)目類(lèi)型:Asp.net web應(yīng)用程序項(xiàng)目 。此時(shí)VS2008自動(dòng)為我們搭建好必要的項(xiàng)目環(huán)境。內(nèi)有兩個(gè)項(xiàng)目,一個(gè)名為MySLbutton.一個(gè)名為MySLbutton.Web,后者為前者的運(yùn)行環(huán)境。為看到初步效果,我們需要做如下工作。
? (1)、先在MySLbutton項(xiàng)目中引入我們前面所建立的名為MyDesignButton的項(xiàng)目中所生成的MyDesignButton.dll(在此項(xiàng)目的bin/debug目錄下)。即我們所建立的自定義控件。
? (2)、修改MySLbutton項(xiàng)目的Page.xaml文件。主要是添加兩處代碼
?? ?? 一是引入xmlns定義。代碼是:?
xmlns:custom="clr-namespace:MyDesignButton;assembly=MyDesignButton"????? 二是引用我們的自定義控件。代碼是:
<custom:MySilverButton?x:Name="MyFirstSLbutton">?</custom:MySilverButton>? 修改后,完整的Page.xaml文件為:
?<UserControl?x:Class="MySLbutton.Page"????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
????xmlns:custom="clr-namespace:MyDesignButton;assembly=MyDesignButton"
?????????????Width="400"?Height="300">
????<Grid?x:Name="LayoutRoot"?Background="White">
????????<custom:MySilverButton?x:Name="MyFirstSLbutton">
????????????
????????</custom:MySilverButton>
????</Grid>
</UserControl>
?(3)、生成項(xiàng)目,并按下F5運(yùn)行,我們可看到初步效果。
下一篇:
SilverLight學(xué)習(xí)筆記--建立Silverlight自定義控件(2)--事件響應(yīng)
轉(zhuǎn)載于:https://www.cnblogs.com/wsdj-ITtech/archive/2009/07/17/1525398.html
總結(jié)
以上是生活随笔為你收集整理的SilverLight学习笔记--建立Silverlight自定义控件(1)--外观设计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Openxml: 导出excel 设置
- 下一篇: 通过Windbg查看DataTable的