【发布时间】:2024-01-18 11:48:01
【问题描述】:
我想知道如何制作一个按钮来加载一个允许我上传 3 张图像的窗口,从而给人一种 gif 的印象。
如果我只做一个项目,一切都很好,但是当我这样做时,我关联的按钮对我不起作用。 这是为什么? 谁能帮我理解一下?
此代码 XAML:
<Image Name="ConnectionImage" Grid.RowSpan="2" Grid.ColumnSpan="2">
<Image.Triggers>
<EventTrigger RoutedEvent="Image.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Name="DoubleAnimation_ConnectionTest" Storyboard.TargetName="ConnectionImage"
Storyboard.TargetProperty="Width" From="200" To="200"
Duration="0:0:1.0" Completed="DoubleAnimation_Completed"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
而图像的代码是:
int ctr = 1;
private void DoubleAnimation_Completed (object sender, EventArgs e)
{
ShowImage();
DoubleAnimation_ConnectionTest.BeginAnimation(Image.WidthProperty, DoubleAnimation_ConnectionTest);
}
private void ShowImage()
{
string filename = "Images/Connection" + ctr + ".jpg";
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(filename, UriKind.Relative);
image.EndInit();
ConnectionImage.Source = image;
ctr++;
if (ctr > 3)
{
ctr = 1;
}
}
和按钮:
private void Button_Test_DB_Click(object sender, RoutedEventArgs e)
{
Window_Test Test_DB = new Window_Test();
Test_DB.Show();
}
【问题讨论】:
-
“对我不起作用”并不是一个真正有用的问题描述。请更具体地说明这一点。并注意:使用动画的 Completed 事件循环执行某些代码看起来真的很奇怪。为什么不直接使用 DispatcherTimer?
-
如果我在一个窗口中做一个只有动画的项目,那么它可以工作,否则从窗口中检索按钮,我会得到一个空的窗口。