Nasıl yapılır: Storyboard ile Animasyonu Bitirdikten Sonra Bir Özelliği Ayarlama
Makale
Bazı durumlarda, animasyon oluşturulduktan sonra bir özelliğin değerini değiştiremeyebilirsiniz.
SolidColorBrush’un rengini animasyonla değiştirin
Aşağıdaki örnekte, bir SolidColorBrushrengine animasyon eklemek için bir Storyboard kullanılır. Düğmeye tıklandığında görsel taslak tetikleniyor.
Completed olayı işlenir, böylece ColorAnimation tamamlandığında programa bildirim gönderilir.
XAML
<ButtonContent="Animate and Then Set Example 1"><Button.Background><SolidColorBrushx:Name="Button1BackgroundBrush"Color="Red" /></Button.Background><Button.Triggers><EventTriggerRoutedEvent="Button.Click"><BeginStoryboard><Storyboard><ColorAnimationStoryboard.TargetName="Button1BackgroundBrush"Storyboard.TargetProperty="Color"From="Red"To="Yellow"Duration="0:0:5"FillBehavior="HoldEnd"Completed="setButton1BackgroundBrushColor" /></Storyboard></BeginStoryboard></EventTrigger></Button.Triggers></Button>
Fırça rengini değiştirme
ColorAnimation tamamlandıktan sonra, program fırçanın rengini mavi olarak değiştirmeye çalışır.
C#
privatevoidsetButton1BackgroundBrushColor(object sender, EventArgs e)
{
// Does not appear to have any effect:// the brush remains yellow.
Button1BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton1BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)
' Does not appear to have any effect:
' the brush remains yellow.
Button1BackgroundBrush.Color = Colors.Blue
End Sub
Önceki kod hiçbir şey yapmıyor gibi görünüyor: fırça sarı kalır ve bu, fırçayı canlandıran ColorAnimation tarafından sağlanan değerdir. Temel alınan özellik değeri (temel değer) aslında maviye dönüştürülür. Ancak etkin veya geçerli değer sarı kalır çünkü ColorAnimation hala temel değerin yerine geçiyor. Temel değerin yeniden etkin değer olmasını istiyorsanız, animasyonunun özelliği etkilemesini durdurmanız gerekir. Bunu görsel taslak animasyonlarıyla yapmanın üç yolu vardır:
Animasyonunun FillBehavior özelliğini Durdur olarak ayarlayın
FillBehaviorStopolarak ayarlayarak, animasyona etkin döneminin sonuna ulaştıktan sonra hedef özelliğini etkilemeyi durdurmasını söylersiniz.
XAML
<ButtonContent="Animate and Then Set Example 2"><Button.Background><SolidColorBrushx:Name="Button2BackgroundBrush"Color="Red" /></Button.Background><Button.Triggers><EventTriggerRoutedEvent="Button.Click"><BeginStoryboard><Storyboard><ColorAnimationStoryboard.TargetName="Button2BackgroundBrush"Storyboard.TargetProperty="Color"From="Red"To="Yellow"Duration="0:0:5"FillBehavior="Stop"Completed="setButton2BackgroundBrushColor" /></Storyboard></BeginStoryboard></EventTrigger></Button.Triggers></Button>
C#
privatevoidsetButton2BackgroundBrushColor(object sender, EventArgs e)
{
// This appears to work:// the brush changes to blue.
Button2BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton2BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)
' This appears to work:
' the brush changes to blue.
Button2BackgroundBrush.Color = Colors.Blue
End Sub
Hikaye tahtasının tamamını kaldır
bir RemoveStoryboard tetikleyicisi veya Storyboard.Remove yöntemi kullanarak görsel taslak animasyonlarına hedef özelliklerini etkilemeyi durdurmalarını söylersiniz. Bu yaklaşım ile FillBehavior özelliğini ayarlama arasındaki fark, görsel taslakları istediğiniz zaman kaldırabilmenizdir, ancak FillBehavior özelliğinin yalnızca animasyon etkin süresinin sonuna ulaştığında bir etkisi olur.
XAML
<ButtonName="Button3"Content="Animate and Then Set Example 3"><Button.Background><SolidColorBrushx:Name="Button3BackgroundBrush"Color="Red" /></Button.Background><Button.Triggers><EventTriggerRoutedEvent="Button.Click"><BeginStoryboardName="MyBeginStoryboard"><Storyboardx:Name="MyStoryboard"><ColorAnimationStoryboard.TargetName="Button3BackgroundBrush"Storyboard.TargetProperty="Color"From="Red"To="Yellow"Duration="0:0:5"FillBehavior="HoldEnd"Completed="setButton3BackgroundBrushColor" /></Storyboard></BeginStoryboard></EventTrigger></Button.Triggers></Button>
C#
privatevoidsetButton3BackgroundBrushColor(object sender, EventArgs e)
{
// This appears to work:// the brush changes to blue.
MyStoryboard.Remove(Button3);
Button3BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton3BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)
' This appears to work:
' the brush changes to blue.
MyStoryboard.Remove(Button3)
Button3BackgroundBrush.Color = Colors.Blue
End Sub
Tek bir özellikten animasyon kaldırma
Animasyonun bir özelliği etkilemesini durdurmak için kullanılan bir diğer teknik de animasyonlu nesnenin BeginAnimation(DependencyProperty, AnimationTimeline) yöntemini kullanmaktır. Animasyonlu özelliği ilk parametre olarak, null ikinci parametre olarak belirtin.
XAML
<ButtonName="Button4"Content="Animate and Then Set Example 4"><Button.Background><SolidColorBrushx:Name="Button4BackgroundBrush"Color="Red" /></Button.Background><Button.Triggers><EventTriggerRoutedEvent="Button.Click"><BeginStoryboard><Storyboard><ColorAnimationStoryboard.TargetName="Button4BackgroundBrush"Storyboard.TargetProperty="Color"From="Red"To="Yellow"Duration="0:0:5"FillBehavior="HoldEnd"Completed="setButton4BackgroundBrushColor" /></Storyboard></BeginStoryboard></EventTrigger></Button.Triggers></Button>
C#
privatevoidsetButton4BackgroundBrushColor(object sender, EventArgs e)
{
// This appears to work:// the brush changes to blue.
Button4BackgroundBrush.BeginAnimation(SolidColorBrush.ColorProperty, null);
Button4BackgroundBrush.Color = Colors.Blue;
}
Private Sub setButton4BackgroundBrushColor(ByVal sender As Object, ByVal e As EventArgs)
' This appears to work:
' the brush changes to blue.
Button4BackgroundBrush.BeginAnimation(SolidColorBrush.ColorProperty, Nothing)
Button4BackgroundBrush.Color = Colors.Blue
End Sub
Bu teknik görsel taslak olmayan animasyonlar için de çalışır.
Bu içeriğin kaynağı GitHub'da bulunabilir; burada ayrıca sorunları ve çekme isteklerini oluşturup gözden geçirebilirsiniz. Daha fazla bilgi için katkıda bulunan kılavuzumuzu inceleyin.
.NET Desktop feedback geri bildirimi
.NET Desktop feedback, açık kaynak bir projedir. Geri bildirim sağlamak için bir bağlantı seçin:
Diğer geliştiriciler ve uzmanlarla gerçek dünyadaki kullanım örneklerini temel alan ölçeklenebilir yapay zeka çözümleri oluşturmak için toplantı serisine katılın.
Veri bağlama ile bir kullanıcı arabirimi oluşturun. Kullanıcı arabiriminiz en son verilere göre otomatik olarak güncelleştirilirken, veriler kullanıcı arabirimindeki değişikliklere yanıt olarak güncelleştirilir.