WPF通过代码更改持续时间

时间:2014-06-06 01:29:46

标签: c# wpf vb.net storyboard

嘿所有我想用以下代码更改DoubleAnimation的持续时间:

 private Duration stay = new Duration(TimeSpan.FromSeconds(1));
    public Duration Stay
    {
        get { return stay; }

        set
        {
            if (stay == value) return;
            stay = value;
            OnPropertyChanged("Stay");
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

我得到的错误是:

  

无法冻结此Storyboard时间轴树以供跨线程使用。

我的XAML看起来像这样:

<EventTrigger RoutedEvent="Window.Loaded" SourceName="NotificationWindow">
   <BeginStoryboard x:Name="FadeInStoryBoard">
      <Storyboard>
         <DoubleAnimation Storyboard.TargetName="NotificationWindow" From="0.01" To="0.85" Storyboard.TargetProperty="Opacity" Duration="0:0:0.8">
         </DoubleAnimation>
         <DoubleAnimation Storyboard.TargetName="NotificationWindow" From="0.85" To="0" Storyboard.TargetProperty="Opacity" Duration="{Binding Path=Stay}" BeginTime="0:0:20" Name="boxDelay" x:Uid="boxDelay1">
         </DoubleAnimation>
      </Storyboard>
   </BeginStoryboard>
</EventTrigger>
   etc etc.....

1 个答案:

答案 0 :(得分:0)

像这样投射对象,即使找到了,也不会起作用。请参阅此示例:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx

最终结果将类似于:

DoubleAnimation theDA = null;
object objFound = stackPanel.FindName("dog");
if (objFound is DoubleAnimation)
{
    theDA = objFound as DoubleAnimation;
    ...
}