Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open Xaml Page From String

Posted on 2010-09-02
12
Medium Priority
?
623 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

671 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