【发布时间】:2020-03-25 17:26:12
【问题描述】:
【问题讨论】:
-
除非绝对必要,否则请不要将信息作为图像共享。请参阅:meta.stackoverflow.com/questions/303812/…、idownvotedbecau.se/imageofcode、idownvotedbecau.se/imageofanexception。
【问题讨论】:
我认为你可以使用这样的代码
Storyboard.SetTargetName(startupAnimation1da, loadingWindow.Name);
代替
startupAnimation1.SetTargetName(startupAnimation1da, loadingWindow.Name);
另外,我认为您的代码不会正确运行,我认为它不会做动画。因为您创建了DoubleAnimation,但没有设置它需要更改的目标属性。您可以使用Storyboard.SetTargetProperty() 方法来设置它:
Storyboard.SetTargetProperty(ANIMATION, PROPERTY);
还有一个例子:
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Button.WidthProperty));
下面是使用Storyboard.SetTargetName() 和Storyboard.SetTargetProperty() 方法的示例。
这是一个改变按钮宽度的动画。
Xaml:
<Window x:Class="WpfTestApp.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Button x:Name="AnimationButton" Height="34" Width="100" Content="Begin" Click="AnimationButton_Click"></Button>
</Window>
C#:
using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Controls;
namespace WpfTestApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void AnimationButton_Click(object sender, RoutedEventArgs e)
{
//Create the animaton
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation
{
From = 100,
To = 300,
Duration = new Duration(new TimeSpan(0, 0, 1)),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
storyboard.Children.Add(doubleAnimation);
//Set taeget name
Storyboard.SetTargetName(doubleAnimation, "AnimationButton");
//Set target property
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Button.WidthProperty));
//Begin the animaton
storyboard.Begin(AnimationButton);
}
}
}
【讨论】:
Storyboard.SetTargetName(startupAnimation1da, loadingWindow.Name);,行吗?