Link to home
Start Free TrialLog in
Avatar of jimseiwert
jimseiwertFlag for United States of America

asked on

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.
Avatar of GMGenius
GMGenius
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of jimseiwert

ASKER

This code looks like winforms and not silverlight. I am building a silverlight page and loading xaml not xml
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

Sorry , i didnt realize, I never realized as there is a tag for Visual Basic .NET also
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

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

Avatar of Gautham Janardhan
Gautham Janardhan

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

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

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

That is what I have done in the current situation but this app can have over 100 pages.
ASKER CERTIFIED SOLUTION
Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked perfect. That was excatly what I needed. Thank you for all of your help.