How to Dynamically Add Object to Viewport? RRS feed

  • Question

  • I have the following code:

        Private Function ParseXAML(ByVal O As Object, ByVal XAML As String) As Object

            Dim newChild As ModelVisual3D
            Dim ThisXAML As String = XAML
            Dim X As XElement


                X = XElement.Load(New StringReader(ThisXAML))
                Dim thisXAMLReader As New XamlReader
                newChild = CType(XamlReader.Load(X.CreateReader), ModelVisual3D)

            Catch ex As Exception

                Return Nothing

            End Try

        End Function

        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button5.Click
            Dim X As XElement = _
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
            <ModelVisual3D x:Name="AmbientLightContainer">
                    <AmbientLight x:Name="AmbientLight" Color="#FF7F7F7F"/>
            <ModelVisual3D x:Name="DirectionalLightContainer">
                    <DirectionalLight x:Name="DirectionalLight" Color="#FF3F3F3F" Direction="0,0,-1">
                            <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="3"/>
            <ModelVisual3D x:Name="DefaultGroup">
                    <GeometryModel3D x:Name="Suzanne" d:Bounds="-1.36718797683716,-0.851562976837158,-0.984375,2.73437595367432,1.70312595367432,1.96875">
                            <MeshGeometry3D Positions="0.46875,0.75781298,-0.24218801 0.4375,0.765625,-0.16406301 503 491 364 491 381 493 504 367 493 367 384 379 381 491 379 491 410 493 384 380 493 380 412"/>
                            <DiffuseMaterial Brush="#FFFFFFFF"/>
                        <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
                        <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1"/>
                        <RotateTransform3D d:EulerAngles="0,0,0"/>

            Dim XPath As String

            'XPath = "./ModelVisual3D/ModelVisual3D/ModelVisual3D.Transform/Transform3DGroup/TranslateTransform3D"

            XPath = "//Transform3DGroup/TranslateTransform3D"

            X.XPathSelectElement(XPath).SetAttributeValue("OffsetX", "5")

            'ParseXAML(V, X.ToString)


            Dim reader As XmlReader = X.CreateReader
            Dim nameTable As XmlNameTable = reader.NameTable
            Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nameTable)
            namespaceManager.AddNamespace("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
            Dim child1 As XElement = X.XPathSelectElement("//Transform3DGroup/TranslateTransform3D", namespaceManager)

            child1.SetAttributeValue("OffsetX", "1")

            namespaceManager.RemoveNamespace("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")

            ParseXAML(V, X.ToString)

        End Sub

    And when I try to run it without this import at the beginning:

    Imports <xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    I get the folloiwng error:

    The tag 'ModelVisual3D' does not exist in XML namespace ''. Line '0' Position '0'.


    newChild = CType(XamlReader.Load(X.CreateReader), ModelVisual3D)

    And if I add the import then i get the following error:

    Object reference not set to an instance of an object.


     X.XPathSelectElement(XPath).SetAttributeValue("OffsetX", "5")

    Could someone help me resolve it please?

    Many thanks

    • Edited by QuddusAli Wednesday, December 17, 2008 3:02 PM
    Saturday, December 6, 2008 12:51 AM

All replies

  • I hope this will help you
    Prasad - www.beautifulmind.blog.co.in
    Saturday, December 6, 2008 2:28 AM
  • Unfortunately I dont think it helps.
    Saturday, December 6, 2008 2:30 AM
  • Is it allowed to bump a thread in this forum?
    Wednesday, December 17, 2008 2:59 PM
  • Howdy,

    It looks like you are trying to write the XAML in your code file and add it?

    XAML lives in XAML files and not code files.

    If you want to add the objects that your XAML is declaring, you need to create instances of them in code and then add those instances as child elements to the relevant parents.

    So, in XAML where you create a button such as

    <Button Content="Hello"/>

    In code you create

    Dim myButton as Button

    My adivce to you is to get yourself a couple of good books on coding (VB.Net is your choice) as well as a good book on understanding XAML and start to learn and understand what both do from the beginning.

    Hope this helps to give you some direction.

    Good luck


    Expression MVP

    Learn Expression Blend at learnexpressionstudio.com

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Thursday, December 18, 2008 1:49 AM
  • thanks for the reply. The method you are suggesting IMHO is a bit long winded. The code i wrote actually works - except when i try to change a value or add the import statement. if i dont try to modify the xelement then i can successfully add the model to the viewport.

    i might be wrong - but there must be a way to what im trying to do.
    Thursday, December 18, 2008 4:03 PM