Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use a copy (or same) object in multiple threads
    primarykey
    data
    text
    <p>I am trying to make a warning window in an application. The window needs to run on a seperate thread and contains among other things a Canvas depicting a failing object. The Canvas already exists in the main application, and what i need is simply to show the same Canvas in the warning window. The problem is that i get an error saying that another thread owns the object. I tried doing a deep copy using <a href="http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlwriter.aspx" rel="nofollow">this</a> method but with no luck. Is there anything i missed, or is there really no simple method to copy a Canvas, or a collection of images. Alternatively, would it be possible to do the deep copy and then change the treading affinity of the copied object?</p> <p>I should think that someone has encountered this problem before, but my serching skills have given me no relevant results this time.</p> <p>Thanks in advance! -ruNury</p> <p>EDIT 1</p> <pre><code> private Canvas cloneCanvas() { Canvas testcanv = new Canvas(); Dispatcher.Invoke(new Action(delegate { var t = SomeViewModel.GetCanvasWithImages(); testcanv = CopyCanvas(t); })); return testcanv; } public static UIElement DeepCopy(UIElement element) { if (element != null) { var xaml = XamlWriter.Save(element); var xamlString = new StringReader(xaml); var xmlTextReader = new XmlTextReader(xamlString); var deepCopyObject = (UIElement)XamlReader.Load(xmlTextReader); return deepCopyObject; } return null; } private Canvas CopyCanvas(Canvas inputCanvas) { if (inputCanvas != null) { var outputCanvas = new Canvas(); foreach (UIElement child in inputCanvas.Children) { outputCanvas.Children.Add(DeepCopy(child)); } return outputCanvas; } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload