A
A
Andrew2015-07-27 20:28:07
WPF
Andrew, 2015-07-27 20:28:07

How to perform different actions with a time interval?

How to make the actions performed in turn and with a time interval of four seconds?
That is, I clicked on the button:

//через 4-е секунды выполнилось это действие
                image2.Source = target;

               //еще четыре секунды следующее 
                image3.Source = target;

               //еще четыре секунды
                image4.Source = target;

               //еще четыре секунды
                image5.Source = target;

All code is like this

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-07-27
@Kaspel

If Sumor 's answer doesn't work for you, try using a timer for WPF . It works in the same thread as the interface, so there will be less problems with it.
XAML code:

<Window x:Class="Animator.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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Image Grid.Row="0" Grid.Column="0" Name="image1" />
        <Image Grid.Row="0" Grid.Column="1" Name="image2" />
        <Image Grid.Row="1" Grid.Column="0" Name="image3" />
        <Image Grid.Row="1" Grid.Column="1" Name="image4" />
    </Grid>
</Window>

Pair code on sharpe:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace Animator
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      images = new Image[] { image1, image2, image3, image4 };

      bitmap1 = new BitmapImage(new Uri(@"http://урл_на_первую_картинку"));
      bitmap2 = new BitmapImage(new Uri(@"http://урл_на_вторую"));
      bitmap3 = new BitmapImage(new Uri(@"http://на_третью"));
      bitmap4 = new BitmapImage(new Uri(@"http://и_на_четвертую"));

      bitmapSources = new BitmapSource[] { bitmap1, bitmap2, bitmap3, bitmap4 };

      timer = new DispatcherTimer(DispatcherPriority.Normal);
      timer.Interval = new TimeSpan(0, 0, 4);
      timer.Tick += Timer_Tick;
      timer.Start();
    }

    int number = 0;

    private void Timer_Tick(object sender, EventArgs e)
    {
      images[number].Source = bitmapSources[number];
      number++;
      if (number >= 4)
      {
        timer.Stop();
      }
    }

    private BitmapSource bitmap1 = null;
    private BitmapSource bitmap2 = null;
    private BitmapSource bitmap3 = null;
    private BitmapSource bitmap4 = null;

    private BitmapSource[] bitmapSources = null;

    private Image[] images = null;

    private DispatcherTimer timer = null;
  }
}

S
Sumor, 2015-07-27
@Sumor

If you work in Wpf, then look towards the standard animation: General information about animation effects
There are objects of the StoryBoard type in which you can describe the sequence of actions for any element of the window.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question