简体   繁体   English

WPF通过代码更改的持续时间

[英]WPF change duration via code

Hey all i am trying to change the duration of a DoubleAnimation with the following code: 嘿,我正在尝试使用以下代码更改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;

The error i get is: 我得到的错误是:

Cannot freeze this Storyboard timeline tree for use across threads. 无法冻结此情节提要板时间轴树以供跨线程使用。

My XAML looks like this: 我的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.....

Casting the object like that, even if found, won't work. 这样的对象即使被发现也无法工作。 See this example: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx . 请参见以下示例: http : //msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.findname.aspx

The end results will look something like: 最终结果将类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM