簡體   English   中英

WPF將XAML動畫轉換為C#代碼

[英]WPF Translating an XAML Animation to C# Code

我在這里找到了很棒的WIPE動畫: http : //www.wearerighteous.com/programming/slidewipe-transition-for-your-windows-in-wpf/

編輯:上面的鏈接已死,因此您必須看到此鏈接http://learnwpf.com/post/2006/10/03/How-can-I-create-a-e2809cwipee2809d-effect-to-transition-between- WPF.aspx中的兩個圖像它具有相同的源代碼。

基本上,那里的代碼是這樣的:

<Window.OpacityMask>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
    <GradientStop Offset="0" Color="Black" x:Name="BlackStop"/>
    <GradientStop Offset="0" Color="Transparent" x:Name="TransparentStop"/>
</LinearGradientBrush> 
<Window.OpacityMask>
    <Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="TransparentStop"
                      Storyboard.TargetProperty="Offset" By="1"  Duration="0:0:1"/>
                    <DoubleAnimation Storyboard.TargetName="BlackStop"
                      Storyboard.TargetProperty="Offset" By="1" Duration="0:0:1"
                      BeginTime="0:0:0.05" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>

我試圖用C#代碼“翻譯”動畫,但似乎無法做到。 我嘗試了多個版本,例如:

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(t);
        OpacityBrush.GradientStops.Add(t2);
        ObjectToAnimate.OpacityMask = OpacityBrush;

        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.SetTarget(DA,TransparentStop);
        Storyboard.SetTarget(DA2,BlackStop);
        Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
        Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
        sb.Begin();
    }

或替換為SetTarget行:

Storyboard.SetTarget(DA, (ObjectToAnimate.OpacityMask as LinearGradientBrush).GradientStops[1]);

但是我的FrameworkElement沒有任何反應。 我稱它為Window(就像該站點中的示例一樣),唯一想到的是它設置了OpacityMask。 動畫的開始和結束(我將帶有MessageBox的Completed Event添加到Storyboard只是為了查看它是否起作用)。 我不知道該怎么辦

請幫忙! :( 有任何想法嗎?

編輯:也許我解釋了這個問題不好。 我正在嘗試用C#代碼創建XAML中的動畫。...我已經嘗試了兩天,正在尋找解決方案。 我不知道為什么C#版本不起作用...

我不確定為什么上面的方法行不通,但是有些方法行之有效-而不是創建一個Storyboard,只需對每個GradientStop使用BeginAnimation()方法即可:

BlackStop.BeginAnimation(GradientStop.OffsetProperty, DA2);
TransparentStop.BeginAnimation(GradientStop.OffsetProperty, DA);
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(t);
    OpacityBrush.GradientStops.Add(t2);
    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();
}

編輯:

這段代碼很棒! 但是它有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);
        }

錯誤行是:

OpacityBrush.GradientStops.Add(t); ->  OpacityBrush.GradientStops.Add(BlackStop);

OpacityBrush.GradientStops.Add(t2); -> OpacityBrush.GradientStops.Add(TransparentStop);

sb.Begin(); ->  sb.Begin(this);

就我而言,似乎效果很好。

 <Window x:Class="WpfHierarchicalDataGrid.Window4"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window4" 
    MinHeight="100"
    MinWidth="100"
    AllowsTransparency="True"
    Opacity="0.7"
    WindowState="Normal"
    WindowStyle="None">
  <Window.OpacityMask>
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
        <GradientStop Offset="0" Color="Black"
                      x:Name="BlackStop"/>
        <GradientStop Offset="0" Color="Transparent"
                      x:Name="TransparentStop"/>
    </LinearGradientBrush>
  </Window.OpacityMask>
  <Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation 
                        Storyboard.TargetName="TransparentStop"
                        Storyboard.TargetProperty="Offset"
                        By="1"  Duration="0:0:1"/>
                    <DoubleAnimation
                       Storyboard.TargetName="BlackStop"
                       Storyboard.TargetProperty="Offset" By="1"
                       Duration="0:0:1"
                       BeginTime="0:0:0.05" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
  </Window.Triggers>
  <Grid>
    <Image Source="Water_lilies.jpg" Stretch="UniformToFill" />
  </Grid>
</Window>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM