【问题标题】:WPF Storyboard Animation is not flashingWPF故事板动画不闪烁
【发布时间】:2016-07-02 20:55:49
【问题描述】:

我一直在按照一些关于如何制作标签闪光的说明进行操作。我在动画方面没有太多经验,所以我不知道我的问题出在哪里!我的代码是:

<Window x:Class="WPFAnimationStoryBoard.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFAnimationStoryBoard"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <Storyboard x:Key="blinkAnimation" RepeatBehavior="1" >
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                          Storyboard.TargetName="InitProc"
                                          AutoReverse="True">
                <ColorAnimationUsingKeyFrames.KeyFrames>
                    <DiscreteColorKeyFrame KeyTime="0:0:0" Value="White"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Black"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:2" Value="White"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:3" Value="Black"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:4" Value="White"/>
                </ColorAnimationUsingKeyFrames.KeyFrames>
            </ColorAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"
                                          Storyboard.TargetName="InitProc"
                                          AutoReverse="True">
                <ColorAnimationUsingKeyFrames.KeyFrames>
                    <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Black"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:1" Value="White"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:2" Value="Black"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:3" Value="White"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:4" Value="Black"/>
                </ColorAnimationUsingKeyFrames.KeyFrames>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </ResourceDictionary>
</Window.Resources>
<Grid>

   <Label Name="InitProc" Grid.Row="0" Grid.Column="2" Content="{Binding ElementName=myWindow, Path=Hello}" Background="White" Foreground="Black">
        <Label.Triggers>
            <EventTrigger RoutedEvent="Binding.TargetUpdated">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <StaticResource ResourceKey="blinkAnimation"/>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Label.Triggers>
    </Label>
</Grid>

这是我的代码:

namespace WPFAnimationStoryBoard
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        Hello = "hello";
    }
    private string _hello;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Hello
    {
        get { return _hello; }
        set
        {
            _hello = value;
            OnPropertyChanged("Hello");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
}

我做错了什么?如果您能提供一些在幕后发生的动画以及答案,我将不胜感激。

谢谢!!

编辑:我更改为允许绑定到属性更新...把球放在那个上。它仍然不会产生闪烁,但它会在 UI 中显示世界“Hello”。

【问题讨论】:

  • 您预计闪烁会在什么时候发生? Label 的任何属性都没有绑定,因此可能永远不会触发 Binding.TargetUpdated 事件。
  • Touchee....我应该看到的!
  • 你美丽美丽的人你!发布答案,以便我接受!

标签: c# wpf animation storyboard


【解决方案1】:

来自 MSDN 上的Binding.TargetUpdated Attached Event 页面:

当一个值从绑定源传输到 绑定目标,但仅适用于与 NotifyOnTargetUpdated 的绑定 值设置为 true。

所以你的绑定应该是这样的:

Content="{Binding ElementName=myWindow, Path=Hello, NotifyOnTargetUpdated=True}" 

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 2013-01-25
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    相关资源
    最近更新 更多