深入探索Silverlight中的样式与行为
立即解锁
发布时间: 2025-08-13 03:16:05 阅读量: 9 订阅数: 36 

### 深入探索Silverlight中的样式与行为
在Silverlight开发中,样式与行为是提升用户界面交互性和可维护性的重要工具。下面将详细介绍如何在Silverlight中使用动作、触发器,并创建自定义行为。
#### 动作与触发器的连接
要使用动作,需要借助触发器。所有触发器都派生自`TriggerBase`,`System.Windows.Interactivity.dll`程序集中包含一个名为`EventTrigger`的触发器,它在特定事件发生时触发。
以下是将动作连接到元素的步骤:
1. **创建Silverlight项目**:首先创建一个新的Silverlight项目。
2. **添加引用**:添加对定义`PlaySoundAction`类的类库和`System.Windows.Interactivity.dll`程序集的引用。
3. **映射命名空间**:在XAML中映射相关命名空间,示例如下:
```xml
<UserControl xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:custom="clr-namespace:CustomBehaviorsLibrary;assembly=CustomBehaviorsLibrary" ... >
```
4. **添加触发器**:使用`Interaction.Triggers`集合为元素添加触发器,例如为按钮添加事件触发器:
```xml
<Button Content="Click to Play Sound">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
```
5. **添加行为**:将行为添加到`EventTrigger.Actions`集合中,示例如下:
```xml
<Button Content="Click to Play Sound">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<custom:PlaySoundAction Source="test.mp3" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
```
#### Expression Blend中的设计时行为支持
在Expression Blend中,使用行为是一种拖放和配置的操作。具体步骤如下:
1. **确保引用**:确保应用程序引用了包含所需行为的程序集和`System.Windows.Interactivity.dll`程序集。
2. **查看行为**:在设计界面绘制一个按钮,点击资产库按钮,然后点击“行为”选项卡,即可查看可用的行为。
3. **添加动作**:从资产库中将动作拖放到控件上,Expression Blend会自动为元素创建触发器并在其中创建动作。
4. **配置对象**:在“对象和时间线”窗格中选择动作,然后在“属性”窗口中调整选项。
默认情况下,当为按钮添加新动作时,Expression Blend会为`Click`事件创建`EventTrigger`。可以通过在动作类中添加`DefaultTrigger`属性来指定默认使用的触发器,示例如下:
```vb
<DefaultTrigger(GetType(UIElement), _
GetType(EventTrigger), New Object() {"MouseLeftButtonDown"})> _
Public Class PlaySoundAction
Inherits TriggerAction(Of FrameworkElement)
...
End Class
```
#### 创建目标触发器
许多动作需要对另一个元素执行操作,此时可以从`TargetedTriggerAction`派生动作类。`TargetedTriggerAction`提供了`Target`属性,应用程序开发人员可以设置该属性,触发器代码可以检查该属性并对目标元素执行必要的操作。
以下是两个派生自`TargetedTriggerAction`的动作示例:
- **FadeOutAction**:对目标元素的不透明度进行动画处理,使其逐渐变为0。
```vb
Public Class FadeOutAction
Inherits TargetedTriggerAction(Of UIElement)
' The default fade out time is 2 seconds.
Public Shared ReadOnly DurationProperty As DependencyProperty = _
DependencyProperty.Register("Duration", GetType(TimeSpan), _
GetType(FadeOutAction), New PropertyMetadata(TimeSpan.FromSeconds(2)))
Public Property Duration() As TimeSpan
Get
Return CType(GetValue(FadeOutAction.DurationProperty), TimeSpan)
End Get
Set(ByVal value As TimeSpan)
SetValue(FadeOutAction.DurationProperty, value)
End Set
End Property
Private fadeStoryboard As New Storyboard()
Private fadeAnimation As New DoubleAnimation()
Public Sub New()
fadeStoryboard.Children.Add(fadeAnimation)
End Sub
Protected Overrides Sub Invoke(ByVal args As Object)
' Make sure the storyboard isn't already running.
fadeStoryboard.Stop()
' Set up the storyboard.
Storyboard.SetTarget(fadeAnimation, Me.Target)
Storyboard.SetTargetProperty(fadeAnimation, New PropertyPath("Opacity"))
' Set up the animation.
' It's important to do this at the last possible instant,
' in case the value for the Duration property changes.
fadeAnimation.To = 0
fadeAnimation.Duration = Duration
fadeStoryboard.Begin()
End Sub
End Class
```
- **FadeInAction**:对目标元素的不透明度进行动画处理,使其逐渐变为1。
```vb
Public Class FadeInAction
Inherits TargetedTriggerAction(Of UIElement)
' The default fade in is 0.5 seconds.
Public Shared ReadOnly DurationProperty As DependencyProperty = _
DependencyProperty.Register("Duration", GetType(TimeSpan), _
GetType(FadeInAction), New PropertyMetadata(TimeSpan.FromSeconds(0.5)))
Public Property Duration() As TimeSpan
Get
Return CType(GetValue(FadeInAction.DurationProperty), TimeSpan)
End Get
Set(ByVal value As TimeSpan)
SetValue(FadeInAction.DurationProperty, value)
End Set
End Property
Private fadeStoryboard As New Storyboard()
Private fadeAnimation As New DoubleAnimation()
Public Sub New
```
0
0
复制全文
相关推荐










