【发布时间】:2016-11-27 00:26:12
【问题描述】:
编辑1:为了满足“Complete, Minimal And Verifiable”示例要求
TL:DR; 故事板根本没有动画。为什么?
我正在尝试创建一个情节提要,它将为渐变中所有渐变停止的偏移设置动画,将它们从左向右移动。
我确定这只是一个愚蠢的语法或参数错误或我的某个地方,但我找不到它。
这是 XAML:
<Window
x:Class="GradientShifting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GradientShiftDerping"
mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"
AllowsTransparency="True" WindowStyle="None">
<Window.Background>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Window.Background>
</Window>
这是后面的代码:
using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace GradientShifting {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private Storyboard _sbGradientShifter = new Storyboard( );
public MainWindow( ) {
InitializeComponent( );
this.Loaded += new RoutedEventHandler(
( S, E ) => this.SetupGradientShift( ) );
}
private void SetupGradientShift( ){
GradientBrush BackBrush = this.Background as GradientBrush;
if ( BackBrush != null ) {
/* Ordering by offset is important because
the last color in the gradient requires
special consideration. */
DoubleAnimationUsingKeyFrames DAUKF;
GradientStopCollection GSC = new GradientStopCollection(
BackBrush.GradientStops.OrderBy( GS => GS.Offset ) );
foreach( GradientStop GS in GSC ){
DAUKF = new DoubleAnimationUsingKeyFrames( ) {
KeyFrames = new DoubleKeyFrameCollection( ){
new LinearDoubleKeyFrame(
1.0D, KeyTime.FromPercent( 1.0D )
}, Duration = TimeSpan.FromSeconds( 3 )
};
//Something I am doing from here...
this._sbGradientShifter.Children.Add( DAUKF );
Storyboard.SetTarget( DAUKF, GS );
Storyboard.SetTargetProperty(
DAUKF, new PropertyPath( GradientStop.OffsetProperty ) );
}
this._sbGradientShifter.Begin( this ); //THIS DOES NOTHING.
}
}
所以,再一次 - 这段代码不起作用。我可以通过调用GradientStop.BeginAnimation 来启动故事板中包含的动画,但是Storyboard.Begin 不起作用。
【问题讨论】:
-
在代码隐藏中正确组装必要的配置可能非常棘手。它在 XAML 中通常更容易,也更易于维护,这是首选方法。如果您确实需要有关代码隐藏版本的帮助,请提供一个好的 minimal reproducible example 可靠地重现特定问题,即您认为 应该 工作但没有工作的代码。我不认为“丑陋的黑客”(正如你所说的那样)有用,但如果你认为它属于问题,请确保它是一个完全独立的代码示例。
-
还要发布
DAUKF里面的内容,因为这将被添加到 SB。 -
@PeterDuniho 请参阅编辑以获取最小、完整和可验证的示例。
-
我已经编辑了您的帖子,以便您发布的代码可以编译。通常,在问题中编辑代码是不受欢迎的,因为它可能会将问题更改为作者想要提出的问题。但在这种情况下,我假设您实际处理的任何代码都确实编译,并且无论出于何种原因,您都没有费心创建一个实际的minimal reproducible example 以包含在您的问题中可以只是复制/粘贴。为了将来参考,请确保您问题中的代码始终是从实际工作示例中直接复制/粘贴的。
标签: c# wpf animation storyboard gradient