Windows Phone 7 创建自定义的控件

简介:

创建自定义的控件:

需要从控件(或 ContentControl)派生,至少,为了继承基本的控件功能,该控件类应从 Silverlight System.Windows.Controls.Control 类派生。但是,它也可以从

ContentControl 和 ItemsControl 等 Control 派生类派生。许多内置控件可以直接或间接从添加了 Content 属性的 ContentControl 派生,而该属性允许对控件的内容(如按压

按钮表面上的内容)进行自定义。ListBox 控件则从 ItemsControl 派生,ItemsControl 可以实现用来向用户提供项目集合的控件的基本行为。因为我们要实现按钮,所以将从

ContentControl 派生。
代码结构如下:
namespace SimpleButtonDemo
{
public class SimpleButton : ContentControl
{
}
}

此时,您已实现了单纯的自定义控件,该控件可在 XAML 文档中通过声明进行实例化。为了说明此问题,将下列语句添加到 Page.xaml:
<local:SimpleButton />
为了使 Silverlight 可以识别此声明,您还需要将以下属性添加到 Page.xaml 的根 User­Control 元素:
xmlns:local="clr-namespace:SimpleButtonDemo;"
您可以看到,clr-namespace 能够识别在其中定义 SimpleButton 类的命名空间,而程序集可以识别包含此控件的程序集。
在xaml中就可以调用该控件了,代码结构如下:
<Grid x:Name="LayoutRoot" Background="White">
<local:SimpleButton />
</Grid>

实例:

 

 

NaiveGradientButton类

 


 
 
  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Controls;  
  4. using System.Windows.Media;  
  5.  
  6. namespace NaiveGradientButtonDemo  
  7. {  
  8.     public class NaiveGradientButton : Button  
  9.     {  
  10.         GradientStop gradientStop1, gradientStop2;  
  11.  
  12.         public NaiveGradientButton()  
  13.         {  
  14.             LinearGradientBrush brush = new LinearGradientBrush();  
  15.             brush.StartPoint = new Point(0, 0);  
  16.             brush.EndPoint = new Point(1, 0);  
  17.  
  18.             gradientStop1 = new GradientStop();  
  19.             gradientStop1.Offset = 0;  
  20.             brush.GradientStops.Add(gradientStop1);  
  21.  
  22.             gradientStop2 = new GradientStop();  
  23.             gradientStop2.Offset = 1;  
  24.             brush.GradientStops.Add(gradientStop2);  
  25.  
  26.             Foreground = brush;  
  27.         }  
  28.  
  29.         public Color Color1  
  30.         {  
  31.             set { gradientStop1.Color = value; }  
  32.             get { return (Color)gradientStop1.Color; }  
  33.         }  
  34.  
  35.         public Color Color2  
  36.         {  
  37.             set { gradientStop2.Color = value; }  
  38.             get { return (Color)gradientStop2.Color; }  
  39.         }  
  40.     }  

xaml中添加引用

xmlns:local="clr-namespace:NaiveGradientButtonDemo"

 


 
 
  1. <!--LayoutRoot is the root grid where all page content is placed--> 
  2.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  3.         <Grid.RowDefinitions> 
  4.             <RowDefinition Height="Auto"/> 
  5.             <RowDefinition Height="*"/> 
  6.         </Grid.RowDefinitions> 
  7.  
  8.         <!--TitlePanel contains the name of the application and page title--> 
  9.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  10.             <TextBlock x:Name="ApplicationTitle" Text="NAIVEGRADIENTBUTTON DEMO" Style="{StaticResource PhoneTextNormalStyle}"/> 
  11.             <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  12.         </StackPanel> 
  13.  
  14.         <!--ContentPanel - place additional content here--> 
  15.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  16.             <StackPanel> 
  17.                 <local:NaiveGradientButton Content="Naive Gradient Button #1" 
  18.                                            HorizontalAlignment="Center" /> 
  19.                   
  20.                 <local:NaiveGradientButton Content="Naive Gradient Button #2" 
  21.                                            Color1="Blue" Color2="Red" 
  22.                                            HorizontalAlignment="Center" /> 
  23.                   
  24.                 <local:NaiveGradientButton Content="Naive Gradient Button #3" 
  25.                                            Color1="{StaticResource PhoneForegroundColor}" 
  26.                                            Color2="{StaticResource PhoneBackgroundColor}" 
  27.                                            HorizontalAlignment="Center" /> 
  28.  
  29.                 <local:NaiveGradientButton Content="Naive Gradient Button #4" 
  30.                                            Style="{StaticResource gradientButtonStyle}" /> 
  31.             </StackPanel> 
  32.         </Grid> 
  33.     </Grid> 

 

本文转自linzheng 51CTO博客,原文链接:

http://blog.51cto.com/linzheng/1078723


相关文章
|
消息中间件 Kafka Windows
Kafka Windows运行错误:创建消费者报错 consumer zookeeper is not a recognized option
Kafka Windows运行错误:创建消费者报错 consumer zookeeper is not a recognized option
693 0
Kafka Windows运行错误:创建消费者报错 consumer zookeeper is not a recognized option
|
2月前
|
C++ Windows
Windows10添加自定义右键菜单VS Code
本文介绍了如何在Windows 10中通过修改注册表,将VS Code添加到右键菜单,实现右键文件、文件夹或空白处时使用VS Code打开。方法同样适用于其他程序,如Sublime Text 3。
|
11月前
|
Windows
.NET 隐藏/自定义windows系统光标
【10月更文挑战第20天】在.NET中,可以使用`Cursor`类来控制光标。要隐藏光标,可将光标设置为`Cursors.None`。此外,还可以通过从文件或资源加载自定义光标来更改光标的样式。例如,在表单加载时设置`this.Cursor = Cursors.None`隐藏光标,或使用`Cursor.FromFile`方法加载自定义光标文件,也可以将光标文件添加到项目资源中并通过资源管理器加载。这些方法适用于整个表单或特定控件。
147 0
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
113 2
|
PHP 开发工具 git
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
118 1
|
安全 Shell Windows
记windows自定义bat脚本自启动
【8月更文挑战第27天】在Windows系统中,可让自定义bat脚本自启动的方法有两种:一是利用“启动”文件夹,通过创建bat脚本的快捷方式并将其放置于该文件夹;二是使用任务计划程序,创建一个启动时触发的任务来运行bat脚本。需确保脚本正确安全,避免对系统产生不良影响。
942 0
|
Java 应用服务中间件 Windows
【Azure 应用服务】App Service for Windows 环境中为Tomcat自定义4xx/5xx页面
【Azure 应用服务】App Service for Windows 环境中为Tomcat自定义4xx/5xx页面
|
存储 安全 搜索推荐
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
204 1
|
XML Go 数据格式
Windows自定义后台进程并设置为开机启动
可以在`Windows`上配置任意一个可执行文件后台启动,并且设置为开机启动。
Windows自定义后台进程并设置为开机启动
|
JavaScript Linux C#
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
306 0