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

Jay*_*sta 5 xamarin.android xamarin xamarin-studio xamarin.forms

我反复收到此错误消息:部分声明不得指定不同的基类。有人可以告诉我这可能是什么原因。这是我的代码。

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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pri*_*ERO 5

作为迟到的...

如果您试图为您的页面创建基类……但它失败了。您可以从您自己的自定义基类继承。

就是这样...

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

<?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
}
Run Code Online (Sandbox Code Playgroud)

2:在你的页面中
引用基础类页面:确保你在“xmlns:views”中引用了你的视图。特别注意页面根元素:“<views:BaseContentPage”

<?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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*nak 3

当您在 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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然两者都是技术问题,而且都是事实,但我认为值得指出的是,如果您继续实例化代码隐藏中的所有控件并更改整个内容属性,则拥有 XAML 文件是没有意义的。:) (3认同)