Solved

Open Xaml Page From String

Posted on 2010-09-02
12
615 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

9 Experts available now in Live!

Get 1:1 Help Now