Improve company productivity with a Business Account.Sign Up

x
?
Solved

Open Xaml Page From String

Posted on 2010-09-02
12
Medium Priority
?
631 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

606 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