views:

267

answers:

2
<Window x:Class="MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication1"
    Title="ContactsSelector" Height="300" Width="300">
    <Window.Content>
        <src:MyPage>
           <!--MyPage is a page that I created and exists in the project--> 
        </src:MyPage>
    </Window.Content>
</Window>

I want to set the content of a window to a page, just like I would do it programmatically:

Dim w As New MyWindow
Dim p As New MyPage
w.Content = p
w.ShowDialog()

Or set it in the Load event of the window, summarily I want it to be done in xaml.

+1  A: 

Try something like this, where MyPageAssembly points to the assembly where your page resides, and MyPage is the name of the Page.

<Window 
    x:Class="MyWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:MyPageAssembly="clr-namespace:MyPage;assembly=MyPageAssembly"
    Title="ContactsSelector" 
    Height="300" 
    Width="300"
    >
    <Window.Content>
        <MyPageAssembly:MyPage />
    </Window.Content>
</Window>
Picky style point: since Content is the content property of Window, you don't need to specify Window.Content: this code is equivalent to <Window><MyPageAssembly:MyPage /></Window>.
itowlson
Does this code before you publish and it worked for you??? cuz I tried it already before asking my question and I get the following error: "Could not create an instance of type 'MyPage'.", Iam douting if your answer would deserve -1 for incorrect information
Shimmy
I have used code like this many times before, and if it didn't work for you, it might have been useful if you had mentioned that in your original question. My mind reading abilities are a bit rusty... Are you sure the xmlns is defined correctly? Did you try adding a breakpoint in your MyPage constructor to see if something in that code throws an exception?
It doesn't even compile. The error I get is a design-time error. The assembly is OK, I even get its children in the intellisense.
Shimmy
+1  A: 

Use a Frame element to show the content of the page.

Marc
Please edit your answer:<Window> <Frame Source="/Pages/MyPage.xaml"/></Window>
Shimmy