WP7 - 使用 Application.Resources 中定义的 Storyboard [英] WP7 - Using Storyboard defined in Application.Resources
问题描述
在我的 Application.Resources
中,我定义了以下 Storyboard
.
In my Application.Resources
I have the following Storyboard
defined.
<Application.Resources>
<!--Storyboard animation for fading out a UI element-->
<Storyboard x:Key="FadeOutAnimation">
<DoubleAnimation From="1"
To="0"
Duration="0:0:0.25"
Storyboard.TargetProperty="Opacity"
AutoReverse="False" />
</Storyboard>
</Application.Resources>
在代码隐藏中,当用户点击它们时,我使用它淡出一些 TextBlock
.
In code-behind I'm using this to fade out some TextBlock
s when the user taps on them.
// Get the storyboard from application resources
Storyboard sb = (Storyboard)App.Current.Resources["FadeOutAnimation"];
// Setup the animation target for fade out
Storyboard.SetTarget( sb.Children.ElementAt( 0 ) as DoubleAnimation, myTextBlock );
// Set the animation completed handler
sb.Completed += ( s, e1 ) => {
// Stop the Storyboard
sb.Stop();
// Hide the TextBlock
myTextBlock.Visibility = Visibility.Collapsed;
};
// Start the Storyboard
sb.begin();
问题是,我是否需要以某种方式解除"myTextBlock
成为 DoubleAnimation
的目标?
The question is, do I need to somehow 'unhook' myTextBlock
from being the target of the DoubleAnimation
?
如果是,我该怎么做?
我问这个问题的原因是我担心在再次使用这个 Storyboard 之前会一直存在对 TextBlock
的引用.
The reason I'm asking is I'm worried about a reference to that TextBlock
hanging around until this Storyboard is used again.
感谢您的帮助!
推荐答案
如果 Xaml 妨碍我们,我们并不总是必须在 sliverlight 中使用它:-
We don't always have to use Xaml in sliverlight if its getting in our way:-
public static AnimationHelper
{
public static void FadeOutAndCollapse(UIElement target)
{
DoubleAnimation da = new DoubleAnimation();
da.From = 1.0;
da.To = 0.0;
da.Duration = TimeSpan.FromSeconds(0.25);
da.AutoReverse = false;
StoryBoard.SetTargetProperty(da, new PropertyPath("Opacity"));
StoryBoard.SetTarget(da, target);
StoryBoard sb = new StoryBoard();
sb.Children.Add(da);
EventHandler eh = null;
eh = (s, args) =>
{
target.Visiblity = Visibility.Collapsed;
sb.Stop();
sb.Completed -= eh;
}
sb.Completed += eh;
sb.Begin();
}
}
有了这个,你可以淡出和折叠任何 UI 元素:-
With this in place you can fade out and collapse any UI element with:-
AnimationHelper.FadeOutAndCollapse(myTextBox);
我倾向于删除 From = 1.0
以使其更通用,以便具有较低起始不透明度的元素在消失之前不会突然闪烁到完全不透明度.
I'd been inclined to remove the From = 1.0
to make it more general so that elements that have a lower starting opacity don't suddenly flash to full opacity before disappearing.
这篇关于WP7 - 使用 Application.Resources 中定义的 Storyboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!