我有几个关于同一类事情的悬而未决的问题,
我对 WPF 很陌生,但对 C# 和 Winforms 很熟悉。
我在互联网上四处寻找一个有效的例子,但还没有找到一个有效的例子。
我想要实现的是在 C# 函数中创建以下内容
- 创建图像(图像 1)
- 创建图像(图像 2)
- 将图片并排放在窗口上
- 创建 Storyboard
- 将 image1 的旋转属性从 0 设置为 360 (animation1)
- 将图像 2 的不透明度属性从完全变为不可见(动画 2)
- Storyboard应该运行 10 秒,动画 1 开始 在 0 秒和动画 2 从 5 秒开始
对代码的明确请求表示歉意,但是,我已经看过并尝试过,我之前的问题有完整的代码执行但没有动画显示(下面的链接)
how to create a storyboard and rotating an image in wpf using c# code
提前致谢
丹.
最佳答案
这是您问题的有效 XAML 版本,后跟 C# 中的相同内容。可能不完全是您想要的,但它应该能说明问题。
XAML 版本:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Storyboard x:Key="Storyboard" BeginTime="00:00:00.000" Duration="00:00:10.000">
<DoubleAnimation Storyboard.TargetName="RotateImage"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
From="0" To="360" BeginTime="00:00:05.000" Duration="00:00:05.000" />
<DoubleAnimation Storyboard.TargetName="OpacityImage"
Storyboard.TargetProperty="Opacity"
From="1" To="0" Duration="00:00:10.000" />
</Storyboard>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image x:Name="RotateImage" Stretch="Uniform" Source="Chrysanthemum.jpg">
<Image.RenderTransform>
<RotateTransform Angle="0" />
</Image.RenderTransform>
</Image>
<Image x:Name="OpacityImage" Grid.Column="1" Stretch="Uniform" Source="Desert.jpg" />
<Button Grid.Row="1" Grid.ColumnSpan="2" Content="Start">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Storyboard="{StaticResource Storyboard}" />
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</Window>
和 C# 版本:
public MainWindow()
{
InitializeComponent();
Image rotateImage = new Image()
{
Stretch = Stretch.Uniform,
Source = new BitmapImage(new Uri("pack://application:,,,/Chrysanthemum.jpg")),
RenderTransform = new RotateTransform()
};
Image opacityImage = new Image()
{
Stretch = Stretch.Uniform,
Source = new BitmapImage(new Uri("pack://application:,,,/Desert.jpg"))
};
LayoutRoot.Children.Add(rotateImage);
LayoutRoot.Children.Add(opacityImage);
Grid.SetColumn(opacityImage, 1);
Storyboard storyboard = new Storyboard();
storyboard.Duration = new Duration(TimeSpan.FromSeconds(10.0));
DoubleAnimation rotateAnimation = new DoubleAnimation()
{
From = 0,
To = 360,
Duration = storyboard.Duration
};
DoubleAnimation opacityAnimation = new DoubleAnimation()
{
From = 1.0,
To = 0.0,
BeginTime = TimeSpan.FromSeconds(5.0),
Duration = new Duration(TimeSpan.FromSeconds(5.0))
};
Storyboard.SetTarget(rotateAnimation, rotateImage);
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
Storyboard.SetTarget(opacityAnimation, opacityImage);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
storyboard.Children.Add(rotateAnimation);
storyboard.Children.Add(opacityAnimation);
Resources.Add("Storyboard", storyboard);
Button button = new Button()
{
Content = "Begin"
};
button.Click += button_Click;
Grid.SetRow(button, 1);
Grid.SetColumnSpan(button, 2);
LayoutRoot.Children.Add(button);
}
void button_Click(object sender, RoutedEventArgs e)
{
((Storyboard)Resources["Storyboard"]).Begin();
}
关于c# - 如何仅使用 C# 代码(在 WPF 窗口内)在图像对象上创建旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073573/