部分声明不得指定不同的基数 类

Partial Declaration must not specify different Base Classes

我反复收到此错误 Message:Partial 声明不得指定不同的基数 类。有人能告诉我这可能是什么原因吗?这是我的代码。

CashAdvancePage.xaml

    <?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="ebmsMobile.CashAdvancePage"
             Title="Cash Advance"
             BackgroundImage="bg3.jpg">
  <Label Text="This is the Cash Advance Page." VerticalOptions="Center" HorizontalOptions="Center" />
</ContentPage>

CashAdvancePage.xaml.cs

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

using Xamarin.Forms;

namespace ebmsMobile
{
    public partial class CashAdvancePage : ViewCell
    {
        public CashAdvancePage()
        {
            //InitializeComponent();
            //NavigationPage.SetHasNavigationBar(this, false);

            var image = new Image
            {
                HorizontalOptions = LayoutOptions.Start
            };
            image.SetBinding(Image.SourceProperty, new Binding("ImageUri"));
            image.WidthRequest = image.HeightRequest = 40;

            var nameLayout = CreateNameLayout();
            var viewLayout = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal,
                Children = { image, nameLayout }
            };
            View = viewLayout;


        }

        static StackLayout CreateNameLayout()
    {
        var nameLabel = new Label
        {
            HorizontalOptions= LayoutOptions.FillAndExpand
        };
        nameLabel.SetBinding(Label.TextProperty, "DisplayName");

        var twitterLabel = new Label
        {
           HorizontalOptions = LayoutOptions.FillAndExpand,
           Font = Fonts.Twitter
        };
        twitterLabel.SetBinding(Label.TextProperty, "Twitter");

        var nameLayout = new StackLayout()
        {
           HorizontalOptions = LayoutOptions.StartAndExpand,
           Orientation = StackOrientation.Vertical,
           Children = { nameLabel, twitterLabel }
        };
        return nameLayout;
    }
    }
}

您在这里混淆了两个不同的东西:页面和视图单元格。

可以通过代码和使用 XAML 创建页面和视图单元格,但它们是不同的东西。

在 XAML 中创建任何组件时,无论是页面还是视图,根节点类型必须是您要子class 的类型。 .cs 文件后面的代码必须从相同的基础 class 派生,或者您实际上可以在代码隐藏中完全省略基础 class,因为部分 classes 不必重述 class 他们的推导。

因此,对于您的 CashAdvance 页面定义,一定要从代码隐藏的 class 定义中删除“:ViewCell”部分。

那么你可能应该在 XAML 中建立你的页面(否则,如果你 bui在代码中解决?)

如果您确实需要自定义视单元(例如,在 ListView 中使用),则为您的视单元创建另一个 .xaml 文件,然后 ui找出它自己的 ui 那里。 然后,您可以在页面 XAML 或代码隐藏中引用它。

有关使用 XAML 的更多详细信息,请查看 the Xamarin XAML documentation

在XAML文件中作为Rootelement需要继承.cs中的ContentPage。

另一个问题是,您需要将 "viewLayout" 分配给内容而不是视图。

using Xamarin.Forms;

namespace ebmsMobile
{
public partial class CashAdvancePage : ContentPage // derive from ContentPage
{
    public CashAdvancePage()
    {
        //InitializeComponent();
        //NavigationPage.SetHasNavigationBar(this, false);

        var image = new Image
        {
            HorizontalOptions = LayoutOptions.Start
        };
        image.SetBinding(Image.SourceProperty, new Binding("ImageUri"));
        image.WidthRequest = image.HeightRequest = 40;

        var nameLayout = CreateNameLayout();
        var viewLayout = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            Children = { image, nameLayout }
        };
        Content = viewLayout; // <-- Set the ViewLayout as Content


    }

    static StackLayout CreateNameLayout()
    {
        var nameLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        nameLabel.SetBinding(Label.TextProperty, "DisplayName");

        var twitterLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
       //     Font = Fonts.Twitter
        };
        twitterLabel.SetBinding(Label.TextProperty, "Twitter");

        var nameLayout = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Orientation = StackOrientation.Vertical,
            Children = { nameLabel, twitterLabel }
        };
        return nameLayout;
    }
}
}

一般来说,保持xaml主标签与.cs文件中的继承相匹配,防止不同基数类错误 所以如果:

<contentPage xmlns="http://xamarin......> </contentPage >

那么 .cs 文件通常会是

public partial class myPage : contentPage { ........}

作为迟到的条目...

以防您尝试为您的页面创建基础 class...但失败了。您可以从您自己的自定义基础 class.

继承

方法如下...

1:创建基础 CLASS 页面(后面有代码 Class)
例如,这是我的,但你的可能不同...

<?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="Pulse.Mobile.Views.BaseContentPage">
</ContentPage>


public partial class BaseContentPage : ContentPage
{
    #region <Fields & Constants>

    protected Style ValidationEntryErrorStyle = Application.Current.Resources["ValidationEntryErrorStyle"] as Style;
    protected Style FormEntryStyle = Application.Current.Resources["FormEntryStyle"] as Style;

    #endregion

    #region <Constructors>

    public BaseContentPage()
    {
        InitializeComponent();
    }

    #endregion

    #region <Events>

    protected override void OnAppearing()
    {
        base.OnAppearing();

        LogHelper.Trace($"Screen - {this}", "Browsed");
    }

    #endregion

    #region <Methods>

    protected bool ValidateKeyPress(Entry entry, TextChangedEventArgs e, string regularExpression)
    {
        var text = e.NewTextValue;

        // Allow Empty
        if(string.IsNullOrWhiteSpace(text))
            return true;

        var result = Regex.IsMatch(e.NewTextValue, regularExpression);
        return result;
    }

    protected void ValidateStyle(Entry control, bool isValid)
    {
        switch (isValid)
        {
            case false:
                control.Style = ValidationEntryErrorStyle;
                break;

            default:
                control.Style = FormEntryStyle;
                break;
        }
    }

    protected void ValidateStyle(Picker control, bool isValid)
    {
        switch (isValid)
        {
            case false:
                control.Style = ValidationEntryErrorStyle;
                break;

            default:
                control.Style = null;
                break;
        }
    }

    #endregion
}

2:在您的页面中引用基础 CLASS 页面
确保在“xmlns:views”中引用您的视图。请特别注意页面根元素:“

<?xml version="1.0" encoding="utf-8" ?>
<views:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       xmlns:views="clr-namespace:Pulse.Mobile.Views;assembly=Pulse.Mobile"
                       x:Class="Pulse.Mobile.Views.ShakeoutDocumentPage">
    <ContentPage.Content>

        // YOUR AWESOME CONTENT GOES HERE...
        
    </ContentPage.Content>
</views:BaseContentPage>