【发布时间】:2012-06-12 11:19:28
【问题描述】:
我想创建一个数据触发器,让我的页面闪烁(从透明变为红色)。所以我创建了一个 DataTrigger 来监听我的视图模型中的一个布尔标志。该标志应指示何时需要提醒用户。在这种情况下,我的页面将从透明闪烁到红色。
我很确定我已经以正确的方式实现了数据触发器,但是我的应用什么也没做 - 没有错误,没有闪烁......所以我一定错过了一些东西。
<Style x:Key="ReminderPage" TargetType="{x:Type ViewTemplates:TpApplicationBarView}" BasedOn="{StaticResource TpApplicationBarViewStyle}">
<Style.Triggers>
<!-- Reminder animation, when the time comes to remind the user -->
<DataTrigger Binding="{Binding IndicateReminderAnimation}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard x:Name="Blink">
<ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
AutoReverse="True"
From="Transparent"
To="Red"
Duration="0:0:1"
RepeatBehavior="Forever">
</ColorAnimation >
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IndicateReminderAnimation}" Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
AutoReverse="False"
To="Transparent"
Duration="0:0:1">
</ColorAnimation >
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
那么,我做错了什么?
更新:我可以在输出窗口中看到以下消息:
System.Windows.Media.Animation Warning: 6 : Unable to perform action because
the specified Storyboard was never applied to this object for interactive control.;
Action='Stop'; Storyboard='System.Windows.Media.Animation.Storyboard';
Storyboard.HashCode='61356140'; Storyboard.Type='System.Windows.Media.Animation.Storyboard';
TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='61356140';
TargetElement.Type='System.Windows.Media.Animation.Storyboard'
Update2: 谷歌搜索后我发现这是 UI 线程的问题。因此,每当我设置绑定属性时,我都会进行调度程序调用。但即使有这个技巧,也没有彩色动画。但是输出窗口中的错误似乎消失了。因此,我正在寻找有关如何修复动画的更多想法。
Update3:设置页面的背景颜色似乎是一个普遍的问题。但这真的很奇怪。页面放置在 NavigationFrame 中。设置导航框架的背景颜色会改变应用程序的颜色,但设置页面的背景颜色(即使没有任何动画)不会改变任何东西。
【问题讨论】:
-
您是否为属性
IndicateReminderAnimation实施了INotifyPropertyChanged?运行时检查VS中的输出窗口,有没有绑定错误? -
嗯。我实现了 INotifyPropertyChanged。但是我查看了输出并发生了以下错误: System.Windows.Media.Animation 警告:6:无法执行操作,因为指定的情节提要从未应用于此对象进行交互式控制。行动='停止'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='61356140'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='61356140'; TargetElement.Type='System.Windows.Media.Animation.Storyboard'
标签: wpf datatrigger coloranimation