0

「ワイプ」アニメーションを変換しようとしています http://learnwpf.com/post/2006/10/03/How-can-I-create-a-e2809cwipee2809d-effect-to-transition-between-two-images-in -C#で使用するWPF.aspxサンプル。

だから私は好きでした:

 <Grid>
        <Image Source="C:\Temp\WMS\Others\megan_fox_17-normal.jpg" />
        <Image Source="C:\Temp\WMS\Others\Hudgens.jpg">
            <Image.OpacityMask>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                    <GradientStop Offset="0" Color="Black" x:Name="BlackStop"/>
                    <GradientStop Offset="0" Color="Transparent" x:Name="TransparentStop"/>
                </LinearGradientBrush>
            </Image.OpacityMask>
        </Image>
        <Button Content="Button" HorizontalAlignment="Left" Margin="72,288,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    </Grid>

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var _doubleAnimationFrontPlayer = new DoubleAnimation();
            _doubleAnimationFrontPlayer.By = 1;
            _doubleAnimationFrontPlayer.Duration = new Duration(TimeSpan.FromSeconds(2));

            sb.Children.Add(_doubleAnimationFrontPlayer);
            Storyboard.SetTargetProperty(_doubleAnimationFrontPlayer, new PropertyPath(GradientStop.OffsetProperty));
            Storyboard.SetTargetName(TransparentStop, "TransparentStop");
            Storyboard.SetTarget(_doubleAnimationFrontPlayer, TransparentStop);

            var _doubleAnimationFrontPlayer2 = new DoubleAnimation();
            _doubleAnimationFrontPlayer2.By = 1;
            _doubleAnimationFrontPlayer2.Duration = new Duration(TimeSpan.FromSeconds(2));
            _doubleAnimationFrontPlayer2.BeginTime =  TimeSpan.FromMilliseconds(1000) ;

            sb.Children.Add(_doubleAnimationFrontPlayer2);
            Storyboard.SetTargetProperty(_doubleAnimationFrontPlayer2, new PropertyPath(GradientStop.OffsetProperty));
           Storyboard.SetTargetName(BlackStop, "BlackStop");
            Storyboard.SetTarget(_doubleAnimationFrontPlayer2, BlackStop);

            sb.Completed += sb_Completed;
            sb.Begin();            
        }

        void sb_Completed(object sender, EventArgs e)
        {
            Debug.WriteLine("DONE");
        }

しかし、何も起こりません... :(

何が欠けているのか手がかりはありますか?

ありがとう!

PSここで同じ質問を見つけるのが遅すぎましたWPFXAMLアニメーションをC#コードに変換する

4

1 に答える 1

0

したがって、ここで見つけた正しいコードWPF XAMLアニメーションをC#コードに変換する

そこに3つのエラーが見つかったので、編集しました。

public void WipeAnimation(FrameworkElement ObjectToAnimate)
{
    LinearGradientBrush OpacityBrush = new LinearGradientBrush();
    OpacityBrush.StartPoint = new Point(1, 0);
    OpacityBrush.EndPoint = new Point(0, 0);
    GradientStop BlackStop = new GradientStop(Colors.Black, 0);
    GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0);
    OpacityBrush.GradientStops.Add(BlackStop);
    OpacityBrush.GradientStops.Add(TransparentStop);
    ObjectToAnimate.OpacityMask = OpacityBrush;

    this.RegisterName("TransparentStop", TransparentStop);
    this.RegisterName("BlackStop", BlackStop);

    Duration d = TimeSpan.FromSeconds(4);
    Storyboard sb = new Storyboard() { Duration = d };
    DoubleAnimation DA = new DoubleAnimation() { By = 1, Duration = d };
    DoubleAnimation DA2 = new DoubleAnimation() { By = 1, Duration = d };
    sb.Children.Add(DA); sb.Children.Add(DA2);
    Storyboard.SetTargetName(DA, "TransparentStop");
    Storyboard.SetTargetName(DA2, "BlackStop");
    Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
    Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
    sb.Begin(this);
}

著者も+1を得ました!:)

于 2012-12-10T15:30:18.753 に答える