Solved

Open Xaml Page From String

Posted on 2010-09-02
12
617 Views
Last Modified: 2013-11-12
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
Comment
Question by:jimseiwert
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 12

Expert Comment

by:GMGenius
ID: 33596811
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
 
LVL 2

Author Comment

by:jimseiwert
ID: 33597495
This code looks like winforms and not silverlight. I am building a silverlight page and loading xaml not xml
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33598956
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 12

Expert Comment

by:GMGenius
ID: 33598964
Sorry , i didnt realize, I never realized as there is a tag for Visual Basic .NET also
0
 
LVL 7

Expert Comment

by:vbigham
ID: 33601307
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
 
LVL 2

Author Comment

by:jimseiwert
ID: 33604114
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
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33653724
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
 
LVL 2

Author Comment

by:jimseiwert
ID: 33674417
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
 
LVL 7

Expert Comment

by:vbigham
ID: 33674686
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
 
LVL 2

Author Comment

by:jimseiwert
ID: 33686716
That is what I have done in the current situation but this app can have over 100 pages.
0
 
LVL 7

Accepted Solution

by:
vbigham earned 500 total points
ID: 33698630
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
 
LVL 2

Author Closing Comment

by:jimseiwert
ID: 33701309
That worked perfect. That was excatly what I needed. Thank you for all of your help.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now