How to link image on canvas with an object

Asked

Viewed 269 times

1

I have the following class:

class Sapo
{
    private Image imgSapo;

    public int IdSapo { get; }

    public Sapo(int id)
    {
        imgSapo = new Image();
        IdSapo = id;
    }

    public Image show
    {
        get 
        {
            imgSapo.Source = new BitmapImage(new Uri("pack://application:,,,/Imagens/sapo.png", UriKind.Absolute));
            imgSapo.Width = 56;
            imgSapo.Height = 56;

            return imgSapo;
        }
    }
}

And I have a method where I create a thread by passing an instance of each Frog class object:

public void CriarSapos()
{
    Thread th;

    int sapos = int.Parse(txt_sapos.Text);

    Sapo[] arraySapos = new Sapo[sapos];

    for (int i = 0; i < sapos; i++)
    {
        th = new Thread(new ParameterizedThreadStart(ThreadImageSapo));
        th.SetApartmentState(ApartmentState.STA);
        th.IsBackground = true;
        arraySapos[i] = new Sapo(th.ManagedThreadId);
        th.Start(arraySapos[i]);
    }
}

The responsible way to insert the images on the canvas:

public void ThreadImageSapo(object obj)
{
    Dispatcher.Invoke(() =>
    {
        Sapo _sapo = (Sapo)obj;

        double max_x = _canvas.ActualWidth - _sapo.show.Width;
        double max_y = _canvas.ActualHeight - _sapo.show.Height;

        Canvas.SetLeft(_sapo.show, rnd.NextDouble() * max_x);
        Canvas.SetTop(_sapo.show, rnd.NextDouble() * max_y);

        _canvas.Children.Add(_sapo.show);

    });
}

How can I get the canvas images and their subject?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.