gpt4 book ai didi

c# - 如何创建继承自 'Page' 类型的 Xamarin Forms 类层次结构?

转载 作者:太空狗 更新时间:2023-10-30 00:29:57 26 4
gpt4 key购买 nike

在 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 会被认为是这种类型,但确实如此。

问题:

  1. 是否可以创建自定义页面的子类?
  2. 为什么将 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。页面B”

我对以下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>

最佳答案

试试这个

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
{
}
}

关于c# - 如何创建继承自 'Page' 类型的 Xamarin Forms 类层次结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44841689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com