2009-09-03 85 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