2009-09-03 40 views
5
<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> 

我想设置一个窗口到页面的内容,就像我会做编程:通过XAML设置Window.Content到页面?

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

或将其设置在窗口的Load事件,简易我希望它是在xaml中完成。

回答

9

使用Frame元素显示页面的内容。

<Window> <Frame Source="/Pages/MyPage.xaml"/> </Window> 
3

尝试这样的事情,在这里MyPageAssembly点到你的页面所在的装配和我的页面是页面的名称。

<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> 
+0

挑剔的风格上看:由于内容是窗口的内容属性,你并不需要指定Window.Content:此代码相当于。 – itowlson 2009-09-03 01:57:33

+0

此代码在发布之前是否适合您?因为我已经试过它之前问我的问题,我得到以下错误:“无法创建类型'MyPage'的实例。”,Iam douting,如果你的答案应该值得-1不正确的信息 – Shimmy 2009-09-03 02:03:30

+0

我已经使用这样的代码以前的时间,如果它不适合你,它可能是有用的,如果你在原来的问题中提到过。我的思维读书能力有点生疏...你确定xmlns被正确定义了吗?您是否尝试在您的MyPage构造函数中添加断点以查看该代码中的某些内容是否会引发异常? – user112889 2009-09-03 03:53:32

相关问题