加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何创建从“页面”类型继承的Xamarin Forms类层次结构?

发布时间:2020-12-15 23:39:38 所属栏目:百科 来源:网络整理
导读:在Xamarin.Forms中,我正在尝试创建一个我然后子类化的页面,如下所示: public partial class PageA : ContentPage { public PageA() {InitializeComponent ();}}public partial class PageB : PageA { public PageB() : base() { ... }} 这两个页面都是包含
在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 does not contain a definition for ‘FindByName’,and the best extension method … requires a receiver of type ‘Element’

这一行:

await Navigation.PushAsync(new PageB());

给出一个错误,即PageB不是Xamarin.Forms.Page.我不知道为什么PageA会被认为是这种类型,但确实如此.

问题:

>是否可以创建自定义页面的子类?
>为什么将ContentPage(PageA)子类视为“Element”类型和“Page”类型的类?为什么PageB不被认为属于那些类型?

我怀疑我在这里的许多事情都很疯狂,所以对我如何表达问题以及指出我应该问什么问题的任何更正都非常受欢迎!

===========编辑

回应以下评论:

网页A

.cs文件(codebehind)具有名称空间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>

网页B

.cs文件(codebehind)具有名称空间AppName.FolderName.SubFolderName,xaml具有x:Class属性值x:Class =“AppName.FolderName.SubFolderName.PageB”

我使用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>

解决方法

试试这个

FYI this code is from my app which is a working example

创建一个基页,如下所示

<?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="Sthotraani.Views.BasePage">

</ContentPage>

你的基页cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace Sthotraani.Views
{
    public partial class BasePage : ContentPage
    {
        public BasePage()
        {
            InitializeComponent();
        }
    }
}

现在派生的页面看起来像这样

<?xml version="1.0" encoding="utf-8" ?>
<views:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:views="clr-namespace:Sthotraani.Views;assembly=Sthotraani"
                x:Class="Sthotraani.Views.LoginPage"
                BackgroundColor="#009688"
                xmlns:controls="clr-namespace:Sthotraani.CustomControls;assembly=Sthotraani"
                xmlns:converters="clr-namespace:Sthotraani.Converters;assembly=Sthotraani"
                xmlns:behaviors="clr-namespace:Sthotraani.Behaviors;assembly=Sthotraani">

</views:BasePage>

派生页面cs看起来像这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace Sthotraani.Views
{
    public partial class LoginPage : BasePage
    {
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读