需要帮助通过定时器在 c# 中随机调用故事板
Need help in calling storyboards randomly in c# via a Timer
好的,我有 4 个故事板和一些动画。我想随机调用它们,但间隔为 2 秒。我尝试了以下方法,它部分起作用了。
TargetAnimate 和 target4animation 被一起调用了两次。
target2animation 和 target4animation 被一起调用了两次。
target3animation 被单独调用了三次。
namespace FF2D
{
/// <summary>
/// Interaction logic for Level1.xaml
/// </summary>
public partial class Level1 : UserControl
{
public Level1()
{
this.InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DispatcherTimer tgtTimer = new DispatcherTimer();
tgtTimer.Tick += new EventHandler(tgtTimer_tick);
tgtTimer.Interval = new TimeSpan(0, 0, 2);
tgtTimer.Start();
}
private void tgtTimer_tick(Object sender, EventArgs e)
{
Random _t = new Random();
int n = _t.Next(1, 4);
if(n == 1)
{
Storyboard sb1 = this.FindResource("TargetAnimate") as Storyboard;
sb1.Begin();
}
if(n == 2)
{
Storyboard sb2 = this.FindResource("target2animation") as Storyboard;
sb2.Begin();
}
if(n == 3)
{
Storyboard sb3 = this.FindResource("target3animation") as Storyboard;
sb3.Begin();
}
else
{
Storyboard sb4 = this.FindResource("target4animation") as Storyboard;
sb4.Begin();
}
}
}
}
target4animation
将在 n = 1、2 或 4 时启动。
将 else
更改为 if(n == 4)
或添加其他内容:
if(n == 1)
{
Storyboard sb1 = this.FindResource("TargetAnimate") as Storyboard;
sb1.Begin();
}
else if(n == 2)
{
Storyboard sb2 = this.FindResource("target2animation") as Storyboard;
sb2.Begin();
}
else if(n == 3)
{
Storyboard sb3 = this.FindResource("target3animation") as Storyboard;
sb3.Begin();
}
else
{
Storyboard sb4 = this.FindResource("target4animation") as Storyboard;
sb4.Begin();
}
好的,我有 4 个故事板和一些动画。我想随机调用它们,但间隔为 2 秒。我尝试了以下方法,它部分起作用了。
TargetAnimate 和 target4animation 被一起调用了两次。 target2animation 和 target4animation 被一起调用了两次。 target3animation 被单独调用了三次。
namespace FF2D
{
/// <summary>
/// Interaction logic for Level1.xaml
/// </summary>
public partial class Level1 : UserControl
{
public Level1()
{
this.InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DispatcherTimer tgtTimer = new DispatcherTimer();
tgtTimer.Tick += new EventHandler(tgtTimer_tick);
tgtTimer.Interval = new TimeSpan(0, 0, 2);
tgtTimer.Start();
}
private void tgtTimer_tick(Object sender, EventArgs e)
{
Random _t = new Random();
int n = _t.Next(1, 4);
if(n == 1)
{
Storyboard sb1 = this.FindResource("TargetAnimate") as Storyboard;
sb1.Begin();
}
if(n == 2)
{
Storyboard sb2 = this.FindResource("target2animation") as Storyboard;
sb2.Begin();
}
if(n == 3)
{
Storyboard sb3 = this.FindResource("target3animation") as Storyboard;
sb3.Begin();
}
else
{
Storyboard sb4 = this.FindResource("target4animation") as Storyboard;
sb4.Begin();
}
}
}
}
target4animation
将在 n = 1、2 或 4 时启动。
将 else
更改为 if(n == 4)
或添加其他内容:
if(n == 1)
{
Storyboard sb1 = this.FindResource("TargetAnimate") as Storyboard;
sb1.Begin();
}
else if(n == 2)
{
Storyboard sb2 = this.FindResource("target2animation") as Storyboard;
sb2.Begin();
}
else if(n == 3)
{
Storyboard sb3 = this.FindResource("target3animation") as Storyboard;
sb3.Begin();
}
else
{
Storyboard sb4 = this.FindResource("target4animation") as Storyboard;
sb4.Begin();
}