0

How to clone a Helix UIElement...?

Anonymous 10 years ago 0
This discussion was imported from CodePlex

BogusException wrote at 2014-08-22 20:08:

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):
Task.Factory.StartNew(
    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.

Thanks!

pat
:)