【发布时间】:2017-06-30 08:09:08
【问题描述】:
在 Xamarin.Forms 中,我正在尝试创建一个页面,然后将其子类化,如下所示:
public partial class PageA : ContentPage {
public PageA() {InitializeComponent ();}
}
public partial class PageB : PageA {
public PageB() : base() { ... }
}
这两个页面都是带有代码隐藏的 xaml 页面,但 PageB 页面无法正常工作,我不知道为什么(我是 XAML、Xamarin、C# 的新手,基本上是一般的编码)。
目前我无法编译代码,因为这一行:
this.FindByName<Label>
给我一个警告:
PageB 不包含“FindByName”的定义,最好的扩展方法...需要一个“元素”类型的接收器
还有这一行:
await Navigation.PushAsync(new PageB());
给出一个错误,即 PageB 不是 Xamarin.Forms.Page。我不知道为什么 PageA 会被认为是这种类型,但确实如此。
问题:
- 是否可以创建自定义页面的子类?
- 为什么将 ContentPage (PageA) 子类的类视为“元素”类型和“页面”类型?为什么 PageB 不属于这些类型?
我怀疑我在这里的很多事情都非常偏离,所以非常欢迎我对问题的措辞方式进行任何更正,并指出我应该问什么问题!
===========编辑
回应以下评论:
PageA
.cs 文件(代码隐藏)具有命名空间 AppName.FolderName,xaml 具有 x:Class 属性值 x:Class="AppName.FolderName.PageA"
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppName.FolderName.PageA">
... (some elements) ...
</ContentPage>
PageB
.cs 文件(代码隐藏)具有命名空间 AppName.FolderName.SubFolderName,xaml 具有 x:Class 属性值 x:Class="AppName.FolderName.SubFolderName.PageB"
我有以下using AppName.FolderName 的引用,这使我可以访问PageA 类
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppName.FolderName.SubFolderName.PageB">
</ContentPage>
【问题讨论】:
-
能否也分享一下 Xaml 代码?以及后面代码的命名空间?
标签: c# xamarin xamarin.forms