views:

13

answers:

1

Hi everyone,

I'm trying to implement the following: I have an Items Manager, that has an Item class inside. Item class can store two possible visual representations of it - BitmapImage(bitmap) and UserControl(vector).

Then later, in the game, I need to share the same image or vector control between all possible places it takes place. For example, consider 10 trees on the map, and all point to the same vector control. Or in some cases this can be bitmap image source.

So, the problem is that BitmapImage source can be easily shared in the application by multiple UIElements. However, when I try to share vector control, it fails, and says Child Element is already a Child element of another control. I want to know how to organize this in the best way. For example replace UserControl with other type of control, or storage, however I need to be sure it supports Storyboard animations inside.

The code looks like this:

    if (bi.item.BitmapSource != null)
    {
        Image previewImage = new Image();
        previewImage.Source = bi.item.BitmapSource;
        itemPane.ItemPreviewCanvas.Children.Add(previewImage);
    } else
    if (bi.item.VectorSource != null)
    {
        UserControl previewControl = bi.item.VectorSource;
        itemPane.ItemPreviewCanvas.Children.Add(previewControl);
    }

Or it is not possible to share same control in different places, then what is the best way to make a copy, or the best way to store vector data.

Thanks in advance

A: 

So, I found the problem. It is possible to attach the same UserControl to different controls.

However, when on update I was deleting control, and then filling up it again with a new pointer, that sometimes was the same as before deleting, somehow it was still in memory. And so it was like 2 same user control attached to the same parent.

I added a line of code that was cleaning all children in control, before updating it with new vector UserControl, and now works like a charm.

Alexander K