1

I can create a new element of type Page in code behind, but I want to add children elements of type UIElement as I would to a Grid like: myGrid.Children.Add(myUIElement);
I don't have the Children property and setting the Content property does not work.
How can I achieve this?

UPDATE:
This is what I have so far, but does not work:

Page myNewPage = new Page();
Grid myGrid = new Grid();
TextBlock myText = new TextBlock();
myText.Text = "I am a TextBlock!";
myGrid.Children.Add(myText);
myNewPage.Content = myGrid;

Frame.Navigate(myNewPage.GetType());
Schrödinger's Box
  • 3,218
  • 1
  • 30
  • 51

1 Answers1

2

A Page can host a single UIElement in its Content property. To add several children, you have to do it like you would do it in XAML: add a panel that can contain and layout several children and add your elements to that panel.

In your question you talk about a grid called myGrid. You could add and layout your items in myGrid and then set myGrid as yourPage.Content.

Your code correctly builds the page. The problem with your code is that your Frame is navigating to a new instance of Page that is not the one that you created. Frame creates a new instance of the type that you pass as a parameter.

If you want to create a page fully in code, you can simply create class that extends Page and build the page in its constructor:

public class MyPage : Page
{
    public MyPage()
    {
        this.BuildPage();
    }

    private void BuildPage()
    {
        var panel = new StackPanel();
        panel.Children.Add(new TextBlock { Text = "Hello" });
        panel.Children.Add(new TextBlock { Text = "World" });

        this.Content = panel;
    }
}

That's, after all, what the InitializeComponent method does in pages created in XAML.

madd0
  • 9,053
  • 3
  • 35
  • 62
  • You mean doing something like: `Grid newGrid = new Grid(); myPage.Content = newGrid; newGrid.Children.Add(myUIElement);`? If so, this does not work, if not, could you please provide an example? – Schrödinger's Box Oct 03 '14 at 08:55
  • you should provide a more complete example of what you are doing to get a better idea from context of what may be going wrong – madd0 Oct 03 '14 at 08:59
  • I've added what I ended up with so far. – Schrödinger's Box Oct 03 '14 at 09:05
  • I edited my answer to explain why your code does not work. The problem is not in the page creation, but in the navigation. – madd0 Oct 03 '14 at 09:12
  • Ok, that makes sense. But if I create a new `Page.xaml` item in my project I don't have this problem, even though I define `typeof` and not the exact `Page.xaml`. How could I set my new Page in code behind to have the content I set by default? – Schrödinger's Box Oct 03 '14 at 09:16
  • When you create `Page.xaml` you are not creating an instance of the page, you are creating the **definition** of the `Page` class. – madd0 Oct 03 '14 at 09:22
  • Nice, that was what I was looking for. However, when I try to navigate to the new MyPage I get an exception `Could not find Windows Runtime type 'Windows.Foundation'.` Couldn't find an answer for that... – Schrödinger's Box Oct 03 '14 at 09:43
  • That's another problem that would deserve another question. – madd0 Oct 03 '14 at 12:03