一起谈.NET技术,Silverlight中自定义控件

  自定义控件并不是一项多么难的技术,关于自定义控件这部分有不少文章讲的很透彻,这里我主要把自己练习自定义控件的过程记录下来。

  这里我以自定义控件BusyPointer为例,首先我们新建一个应用程序,命名为CustomControl,这里我们将自定义控件放入单独的项目中,所以在解决方案里添加一个Silverlight Class Library项目,命名为BusyPointer,现在我们把Class1.cs类删除,然后在BusyPointer项目中添加一个Silverlight Template Control,我们为之命名为BusyPoint,这时架构如下图所示,项目中增加了一个类文件,同时增加了名为Generic的xaml文件。             捕获  现在我们开始定义控件逻辑对于BusyPoint这个控件,这里我们只是简单模仿Win7里一个鼠标行为在BusyPoint.cs文件中给其定义了一个依赖属性。

 
  
public bool IsBusy
{
get
{
return ( bool )GetValue(IsBusyProperty);
}
set
{
SetValue(IsBusyProperty, value);
ChangeState();
}
}
private void ChangeState()
{
if (IsBusy) VisualStateManager.GoToState( this , " Busied " , true );
else VisualStateManager.GoToState( this , " Normal " , true );
}
public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register(
" IsBusy " , typeof ( bool ), typeof (BusyPoint), new PropertyMetadata( new PropertyChangedCallback(OnBusyChanged)));
private static void OnBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
((BusyPoint)sender).IsBusy
= ( bool )e.NewValue;
}

  关于依赖属性这个部分就不细说了,这里我们声明了一个用于当IsBusy值发生变化时的回调方法OnBusyChanged,现在我们在Generic.xaml中定义控件的样式。

 
  
< Style TargetType ="local:BusyPoint" >
< Setter Property ="Template" >
< Setter.Value >
< ControlTemplate TargetType ="local:BusyPoint" >
< Grid x:Name ="LayoutRoot" Background ="Transparent" >
< vsm:VisualStateManager.VisualStateGroups >
< vsm:VisualStateGroup x:Name ="CommonStates" >
< vsm:VisualState x:Name ="Normal" />
< vsm:VisualState x:Name ="Busied" >
< Storyboard >
< DoubleAnimationUsingKeyFrames Storyboard.TargetName ="VisualElement" Storyboard.TargetProperty ="(UIElement.RenderTransform).Angle" RepeatBehavior ="Forever" >
< SplineDoubleKeyFrame KeyTime ="0:0:1" Value ="360" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ vsm:VisualState >
</ vsm:VisualStateGroup >
</ vsm:VisualStateManager.VisualStateGroups >
< Ellipse Width ="20" Height ="20" StrokeThickness ="5.5" x:Name ="VisualElement" >
< Ellipse.Stroke >
< LinearGradientBrush EndPoint ="0.5,1" StartPoint ="0.5,0" >
< GradientStop Color ="#FF096475" Offset ="0.571" />
< GradientStop Color ="#FFA8FCFC" Offset ="1" />
</ LinearGradientBrush >
</ Ellipse.Stroke >
< Ellipse.RenderTransform >
< RotateTransform CenterX ="12.5" CenterY ="12.5" />
</ Ellipse.RenderTransform >
</ Ellipse >
</ Grid >
</ ControlTemplate >
</ Setter.Value >
 
</ Style >

  这里我们还需要添加一下命名空间xmlns:vsm="clr:namespace:System.Windows;assembly=System.Windows"

  这样我们自定义控件就完成了,下面我们在MainPage中使用这个控件,编译之后,我们就可以在CustomControl中引用BusyPointer.dll                捕获

  dll添加完成后,我们在MainPage.xaml页面中添加一个引用xmlns:by="clr-namespace:BusyPointer;assembly=BusyPointer",UI代码如下:

 
  
< by:BusyPoint x:Name ="busy" IsBusy ="False" ></ by:BusyPoint >
< Button Content ="Button" Name ="button1" Click ="button1_Click" />

  这里我们将BusyPoint控件的IsBusy设为False,我们通过这个Button将其设为True。

 
  
this .busy.IsBusy = true ;

  现在看一看页面的效果:             捕获

  虽然比起系统自带的鼠标头不怎么优雅,当然这个部分并不是重点了,只是以这个控件为例,将自定义控件的步骤梳理了一遍。

  代码下载:CustomeControl.rar   VS2010+SL3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值