我在我的 C#/XAML WinRT 应用程序中使用托管在服务器上的图像。当下载该图像时,我希望它淡入。我注意到FadeInThemeAnimation
这是我希望使用的。但是,我想像使用它一样使用它EntranceThemeTransition
。有没有办法做到这一点?如果是这样,如何?
问问题
6149 次
3 回答
11
我遇到了同样的问题,但找到了解决方案,我认为分享它可能仍然有用。
显然FadeInThemeAnimation
是一种特殊的动画,它不像你想象的那样在不透明度和可见性上起作用,而是在项目的 RenderTransform 上起作用。我只有在使用FadeOutThemeAnimation
.
但这里有一个解决方法。在 XAML 中,将 Storyboard 添加到图像容器的资源中,如下所示:
<Grid>
<Grid.Resources>
<Storyboard x:Name="ImageFadeInStoryboard">
<DoubleAnimation From="0" To="1" Storyboard.TargetName="yourImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.6" />
</Storyboard>
</Grid.Resources>
<Image x:Name="yourImage" Source="{Binding ...}"/>
...
然后为图像的ImageOpened
事件添加一个处理程序:
<Image x:Name="yourImage" Source="{Binding ...}" ImageOpened="OnImageOpened"/>
在代码隐藏中:
private void OnImageOpened(object sender, RoutedEventArgs e)
{
ImageFadeInStoryboard.Begin();
}
希望有帮助:)
于 2012-09-13T14:25:24.563 回答
3
另一种方法是创建一个附加的行为来“监听”一个布尔绑定来触发主题动画:
static class VisibilityAnimationBehavior
{
public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.RegisterAttached( "IsVisible", typeof( bool ), typeof( VisibilityAnimationBehavior ), new PropertyMetadata( true, IsVisibleChanged ) );
public static bool GetIsVisible( DependencyObject Target ) { return ( bool )Target.GetValue( IsVisibleProperty ); }
public static void SetIsVisible( DependencyObject Target, bool Value ) { Target.SetValue( IsVisibleProperty, Value ); }
static void IsVisibleChanged( DependencyObject Source, DependencyPropertyChangedEventArgs Arguments )
{
bool OldValue = ( bool )Arguments.OldValue;
bool NewValue = ( bool )Arguments.NewValue;
DependencyObject ParentObject = Source as DependencyObject;
if( ParentObject == null )
return;
if( NewValue == true && OldValue != true )
{
Storyboard TransitionStoryboard = new Storyboard();
Storyboard.SetTarget( TransitionStoryboard, ParentObject );
TransitionStoryboard.Children.Add( new FadeInThemeAnimation() );
TransitionStoryboard.Begin();
}
else if( NewValue == false && OldValue != false )
{
Storyboard TransitionStoryboard = new Storyboard();
Storyboard.SetTarget( TransitionStoryboard, ParentObject );
TransitionStoryboard.Children.Add( new FadeOutThemeAnimation() );
TransitionStoryboard.Begin();
}
}
}
为了将行为附加到 XAML DependencyObject(此示例中为 Grid),请使用以下命令:
<Grid local:VisibilityAnimationBehavior.IsVisible="{Binding Path=TheBooleanBinding}">
于 2015-04-15T09:02:23.710 回答
0
这不是解决方案,但我希望对您有所帮助,
在 XAML 中使用如下内容:
<StackPanel>
<StackPanel.Resources>
<Storyboard x:Name="EnterStoryboard">
<FadeOutThemeAnimation Storyboard.TargetName="MyImage" />
</Storyboard>
<Storyboard x:Name="ExitStoryboard">
<FadeInThemeAnimation Storyboard.TargetName="MyImage" />
</Storyboard>
</StackPanel.Resources>
<Image x:Name="MyImage"
PointerEntered="MyImage_PointerEntered"
PointerExited="MyImage_PointerExited"
Fill="Blue" Width="200" Height="300" />
</StackPanel>
在代码中:
private void MyImage_PointerEntered(object sender, PointerRoutedEventArgs e)
{
EnterStoryboard.Begin();
}
private void MyImage_PointerExited(object sender, PointerRoutedEventArgs e)
{
ExitStoryboard.Begin();
}
于 2018-09-23T06:25:52.787 回答