我正在嘗試爲我的WP應用創建自定義基頁。我通過創建從PhoneApplicationPage
繼承這樣的新cs
類文件,這樣做:將控制權添加到自定義基頁
public class BasePage: PhoneApplicationPage
{
//handle common non-visual stuff here
}
的問題是,我想使用BasePage
每個頁面上添加一個控制,但BasePage的沒有LayoutRoot
或任何視覺元素,我可以附加控制。有沒有辦法將相同的控件添加到使用BasePage的所有頁面,以便我不必每次都複製/粘貼它?
編輯:根據TriggerPin的答案添加XAML。我創建了一個新的XAML文件我BasePage.cs
但這是BasePage.g.cs
引發錯誤: 「MyApp.MainPage已經包含了‘_contentLoaded’的定義」(即「LayoutRoot」和「的InitializeComponent」及類似消息)
<local:BasePage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.Template"
x:Class="MyApp.MainPage"
mc:Ignorable="d">
this.Children.Add – ebattulga 2013-02-15 17:52:03
@ebattulga - 我試過了,但是'Children'沒有出現在Intellisense中。 (編譯結果「不包含兒童定義...」) – XSL 2013-02-15 17:53:55
創建網格或容器控件並設置爲this.Content = yourGrid; – ebattulga 2013-02-15 19:19:55