Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Open Xaml Page From String

I have a drop down list of xaml pages in my project on my main form. When a user selects one I want to create a new tab and load the content of teh tab with my xaml page they selected. I have the new tab being created but not sure how to load teh content of teh tab with that xaml page. Any help is appreciated.
0
jimseiwert
Asked:
jimseiwert
  • 5
  • 3
  • 2
  • +2
1 Solution
 
GMGeniusCommented:
Hi
Add in the tab a web control and set the web.navigate to the XML content
An example of adding a tab and a web control on the tab below:-

Dim oTab As New TabPage
oTab.Name = "NewTab" 
oTab.Text = "NewTab"
oTab.Parent = Me.TabControl1 ' Your Tab Control name
Me.Controls.Add(oTab)
webReport.Parent = oTab
webReport.Dock = DockStyle.Fill
webReport.Navigate(sXMLContent)

xXMLContent is the XML data

Open in new window

0
 
jimseiwertAuthor Commented:
This code looks like winforms and not silverlight. I am building a silverlight page and loading xaml not xml
0
 
Bob LearnedCommented:
In order to create a control graph from a XAML string, I would look at the XamlReader class, which is in the System.Windows.Markup namespace.

XamlReader Class
http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader.aspx
' Load the button
Dim stringReader As New StringReader(savedButton)
Dim xmlReader As XmlReader = XmlReader.Create(stringReader)
Dim readerLoadButton As Button = CType(XamlReader.Load(xmlReader), Button)

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GMGeniusCommented:
Sorry , i didnt realize, I never realized as there is a tag for Visual Basic .NET also
0
 
vbighamCommented:
Are you talking about the Silverlight TabControl, and you have created the TabItem but want to add your xaml "page" as a child to that tab Item?

Assuming that this is what you are trying to do, I think this code snippet might help.  Let me know if it you are trying to do something different.
Dim tItem As New TabItem

tItem.Content = New MyXamlPage

Open in new window

0
 
jimseiwertAuthor Commented:
vbigham that is excatly what I wanted regarding the content. Thank you. The only part of the question left is how can I sent the xaml page to use a string passed in somewhat like the example below


sub opennewpage (pagename as string)

Dim tItem As New TabItem
tItem.Content = New pagename 

end sub

Open in new window

0
 
Gautham JanardhanCommented:
combining TheLearnedOne post above and yours we get
sub opennewpage (pagename as string)

Dim stringReader As New StringReader(pagename )
Dim xmlReader As XmlReader = XmlReader.Create(stringReader)
Dim tItem As New TabItem
tItem.Content = CType(XamlReader.Load(xmlReader), UIElement)

end sub

Open in new window

0
 
jimseiwertAuthor Commented:
but using that method it will not bring the backend code and everything else over from the silverlight page. In my winforms app i did the following
Dim obhNew As Runtime.Remoting.ObjectHandle
obhNew = Activator.CreateInstance(Nothing, pagename)
Dim frmNew As Form = DirectCast(obhNew.Unwrap, Form)

Open in new window

0
 
vbighamCommented:
If you don't have too many pages I would be inclined to Select Case based on the page name string, and load the appropriate one.  If you are building these pages on the fly, then that is when you would probably need a xamlReader, and wiring up all the code/event handlers sounds interesting.

Here is what I would do, as long as you are building these pages at design time:
Sub opennewpage(ByVal pagename As String)
        Dim tItem As New TabItem

        Select Case pagename.Trim.ToLower()
            Case "page1.xaml", "page1"
                tItem.Content = New Page1

            Case "page2.xaml", "page2"
                tItem.Content = New Page2

            Case "page3.xaml", "page3"
                tItem.Content = New Page3

            Case "page4.xaml", "page4"
                tItem.Content = New Page4

            Case Else
                Throw New Exception("Invalid Page Request: " & pagename & " cannot be found")
        End Select
    End Sub

Open in new window

0
 
jimseiwertAuthor Commented:
That is what I have done in the current situation but this app can have over 100 pages.
0
 
vbighamCommented:
So, it sounds like what you want to do is find the associated class inside the assembly so that you can easily add/remove many many pages without hardcoding them.  

I did a little research and found this method.  I created a new Silverlight business template application and did the following in the New() method of the main page, when it ran all of the MainPage navigation stuff was replaced by the TabControl with the newly created TabItems.  The test buttons that I put on those pages worked!  

As you will see, Activator.CreateInstance will throw a NullArgumentException if the type cannot be found by Type.GetType()..

Hope this is what you need..
Public Sub New()
        InitializeComponent()

        Dim t As Type = Me.GetType()
        Dim assemblyName As String = t.Namespace

        Dim obj1 As Object
        obj1 = Activator.CreateInstance(Type.GetType(assemblyName & "." & "About"))

        Dim obj2 As Object

        Try
            obj2 = Activator.CreateInstance(Type.GetType(assemblyName & "." & "Home2"))
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            obj2 = Activator.CreateInstance(Type.GetType(assemblyName & "." & "Home"))
        End Try

        LayoutRoot.Children.Clear()

        Dim tControl As New TabControl
        Dim tItem1 As New TabItem With {.Header = "About"}
        Dim tItem2 As New TabItem With {.Header = "Home"}

        tControl.Items.Add(tItem1)
        tControl.Items.Add(tItem2)

        tItem1.Content = obj1
        tItem2.Content = obj2

        LayoutRoot.Children.Add(tControl)
    End Sub

Open in new window

0
 
jimseiwertAuthor Commented:
That worked perfect. That was excatly what I needed. Thank you for all of your help.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now