How to clone a Helix UIElement...?

Aug 22, 2014 at 6:08 PM
Edited Aug 22, 2014 at 6:13 PM
How can I change this stock (non-Helix) function to work with things like CubeVisual3D?

Right now I get

Value of type 'Helixtoolkit.Wpf.CubeVisual3D' cannot be converted to 'System.Windows.UIElement'. ...which makes sense. Any thoughts?
  • To Be Clear: I don't want to change the cube's type at all, but merely be able to clone it in the way below, returning the correct UIElement equivalent in the Helix namespace...*
    Public Function CloneElement(ByVal orig As UIElement) As UIElement
        If orig Is Nothing Then
            Return (Nothing)
        End If
        Dim s As String = System.Windows.Markup.XamlWriter.Save(orig)
        Dim stringReader As New StringReader(s)
        Dim xmlReader As XmlReader = XmlTextReader.Create(stringReader, New XmlReaderSettings())
        Return CType(System.Windows.Markup.XamlReader.Load(xmlReader), UIElement)
    End Function
The call to this function looks like this (pardon this editor's formatting):
    Function() { 
        Dispatcher.BeginInvoke(New Action(Sub()
            Dim clone As CubeVisual3D = CType(CloneElement(cube1), CubeVisual3D)
            Messenger.Default.Send(Of HelixToolkit.Wpf.CubeVisual3D)(cube1) ' sends to AddAChildMvvm()
    End Sub))})
It's a threading problem, and the Function creates an instance of the cube in thsi thread, not the thread it was originally created under/in.