从代码隐藏设置目标名称

从代码隐藏设置目标名称

本文介绍了故事板 - 从代码隐藏设置目标名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有故事板资源





<窗​​口的.resources>
<情节提要X:键=衰落Storyboard.TargetName =NotifyWindowStoryboard.TargetProperty =透明度>
将; DoubleAnimation是从=1要=0时长=0:0:1>
< / DoubleAnimation是>
< /故事板>
< /Window.Resources>

和上的windowClosing我有下面的代码



 私人无效NotifyWindow_Closing(对象发件人,System.ComponentModel.CancelEventArgs E)
{
故事板褪色= this.Resources [衰落]作为故事板;
如果(衰减= NULL&放大器;!&安培;!fadingCompleted)
{
fading.Completed + = FadingStoryBoard_Completed;
fading.Begin();
e.Cancel = TRUE;
}
}

私人无效FadingStoryBoard_Completed(对象发件人,EventArgs五)
{
fadingCompleted = TRUE;
关闭();
fadingCompleted = FALSE;
}

这工作得很好,但我想这个故事板移动到另一个组件。所以我需要指定 StoryBoard.TargetName 表单代码。我如何能做到这一点。


解决方案

附加属性可以通过名为静态辅助方法代码进行设置: 设置+属性名



请参阅C#示例的:

  Storyboard.SetTargetName( yourAnimationNotifyWindow); 


I have StoryBoard in resources

  <Window.Resources>
    <Storyboard x:Key="Fading" Storyboard.TargetName="NotifyWindow" Storyboard.TargetProperty="Opacity" >
        <DoubleAnimation From="1" To="0" Duration="0:0:1">
        </DoubleAnimation>
    </Storyboard>
  </Window.Resources>

And on WindowClosing I have next code

private void NotifyWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Storyboard fading = this.Resources["Fading"] as Storyboard;
    if (fading != null && !fadingCompleted)
    {
        fading.Completed += FadingStoryBoard_Completed;
        fading.Begin();
        e.Cancel = true;
    }
}

private void FadingStoryBoard_Completed(object sender, EventArgs e)
{
   fadingCompleted = true;
   Close();
   fadingCompleted = false;
}

And this works fine, But I want to move this storyboard to another assembly. So i need to specify StoryBoard.TargetName form code. How can I do that?

解决方案

Attached properties can be set from code via static helper methods named: "Set" + PropertyName

See C# example here:

Storyboard.SetTargetName(yourAnimation, "NotifyWindow");

这篇关于故事板 - 从代码隐藏设置目标名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:17