1

我正在尝试为我的 WP 应用程序创建一个自定义基页。我通过创建一个cs继承自这样的新类文件来做到PhoneApplicationPage这一点:

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">
4

2 回答 2

2

请注意,默认情况下,派生自 PhoneApplicationPage 的类型是部分的

 public partial class MainPage : PhoneApplicationPage

部分定义允许您将类定义拆分到多个文件中。最常见的是这种类型,定义在 .cs 和 .xaml 文件之间进行拆分。

 <phone:PhoneApplicationPage x:Class="MyProject.MainPage"
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">

     <!-- Custom layout here -->

 </phone:PhoneApplicationPage>

如果您使您的实现成为部分类并提供基本的 xaml 代码,您将能够做您想要实现的事情。

于 2013-02-15T18:45:29.113 回答
0

您需要一个至少必须是 Panel 类型的容器才能添加您的子控件。Page 只是一个用户控件,不能直接将您的 UIElement 添加到其中。

尝试这个:

Panel container = // init it from your page, you must have a root element in page.
container.Children.Add(new TextBlock()); // your children control.

我是在win8里做的。

于 2014-05-06T10:17:45.090 回答