[英]How to animate a resource in XAML?
在XAML文檔中,我有一個漸變畫筆作為資源和一堆使用此資源的形狀。 我想使用故事板為畫筆設置動畫,但我不知道如何將資源中的畫筆設置為故事板的目標。 僅使用其名稱不起作用,{StaticResource name}也不起作用。 它甚至可能嗎?
我更喜歡只有XAML的解決方案,但如果不能解決問題,我將使用代碼隱藏。 如果它讓我離開Storyboard.Target和Storyboard.TargetProperty未分配。
編輯:我想動畫畫筆的漸變停止。 問題是,當它不是資源時,我可以輕松地為它設置動畫,但是直接應用於對象。 我可以點擊Expression Blend來做到這一點。 我只是不知道如何動畫它的資源(即在下面的代碼中放置什么而不是??(故事板是為矩形創建的))
code:
<UserControl.Resources>
<LinearGradientBrush x:Key="Outline" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#7F7CE3FF" Offset="0"/>
<GradientStop Color="#7F047695" Offset="1"/>
<GradientStop Color="#FFFFFFFF" Offset="0.942"/>
</LinearGradientBrush>
<Storyboard x:Key="Glitter">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="??" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:02.6000000" Value="0.529"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
...
它可以直接為背景/填充屬性設置動畫,使用要作為Storyboard.TargetName設置動畫的對象名稱(例如Rectangle):
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.Resources>
<LinearGradientBrush x:Key="Outline" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#7F7CE3FF" Offset="0"/>
<GradientStop Color="#7F047695" Offset="1"/>
<GradientStop Color="#FFFFFFFF" Offset="0.942"/>
</LinearGradientBrush>
</Grid.Resources>
<Border Name="border"
Background="{StaticResource Outline}"
Width="200" Height="200" />
</Grid>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="border"
Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
<SplineDoubleKeyFrame KeyTime="00:00:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:1" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
編輯
從代碼背后似乎是完全可能的:
XAML:
<Grid Name="grid">
<Grid.Resources>
<LinearGradientBrush x:Key="Outline" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#7F7CE3FF" Offset="0"/>
<GradientStop Color="#7F047695" Offset="1"/>
<GradientStop Color="#FFFFFFFF" Offset="0.942"/>
</LinearGradientBrush>
</Grid.Resources>
<Border Background="{StaticResource Outline}"
Width="100" Height="100" HorizontalAlignment="Left" />
<Border Background="{StaticResource Outline}"
Width="100" Height="100" HorizontalAlignment="Right" />
</Grid>
C#代碼背后:
LinearGradientBrush b = grid.Resources["Outline"] as LinearGradientBrush;
b.GradientStops[0].BeginAnimation(GradientStop.OffsetProperty, new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1))));
您無法為Brush類型的屬性設置動畫,您只能為具有適當動畫類的類型設置動畫,例如DoubleAnimation,PointAnimation或ColorAnimation(請注意,最后一個動畫類型為Color的屬性,而不是Brush)。
但是,某些Brushes具有double類型的DependencyProperties,您可以設置動畫,例如LinearGradientBrush-Class的StartPoint和EndPoint屬性。
如果你能詳細說明動畫應該做什么,也許我們可以找到一個解決方法。
編輯:要為畫筆設置動畫,必須在動畫觸發器的范圍內聲明它,例如在Data-或ControlTemplate中。 通過其密鑰動畫資源將不起作用。
不確定你正在嘗試在畫筆內部制作動畫,但動畫畫筆資源可能非常棘手。 我沒有時間輸入所有內容,但這里有一些關於如何處理它的“教程”:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.