【IT168 技术】Silverlight 3设计工具为我们提供了一项Behavior(动作)功能,他可以使设计人员轻松的做出一些很酷的程序。如果没有这个功能,好多事情都要依赖于重写代码来解决。为了方便编写、访问和使用该功能目的,在Expression Blend 3 和 Visual Studio 2008都可以进行模板的设定,我们可以把以前自己所写的东西设定成模板,也可以利用别人设计制作好并共享的模板。Silverlight 3程序提供了一些模板的范例,供大家参考。
我们可以通过很少的操作来调用这些模板的代码。其中包括Behavior、Action和 Trigger。
图1 在Blend中调用
图2 在VS2008中调用
模板的示例代码:
public class Trigger1 : TriggerBase <DependencyObject>
{
protected override void OnAttached()
{
base.OnAttached();
// Insert code that you want to run when the Trigger is attached to an object.
}
protected override void OnDetaching()
{
base.OnDetaching();
// Insert code that you would want run when the Trigger is removed from an object.
}
//To invoke any associated Ations when this Trigger gets called, use this.
//InvokeActions (o) where o is an object that you can pass in as a parameter
}
{
protected override void OnAttached()
{
base.OnAttached();
// Insert code that you want to run when the Trigger is attached to an object.
}
protected override void OnDetaching()
{
base.OnDetaching();
// Insert code that you would want run when the Trigger is removed from an object.
}
//To invoke any associated Ations when this Trigger gets called, use this.
//InvokeActions (o) where o is an object that you can pass in as a parameter
}
模块代码采用了DLL动态链接库的形式,使用 DLL 有助于促进代码的模块化、代码重用、内存的有效使用和减少所占用的磁盘空间。因此,操作系统和程序能够更快地加载和运行,并且在计算机中占用较少的磁盘空间。让我们来具体操作一下。在我们的项目中添加模板代码。
图3 下载的模板
图4 在项目中添加