Partial Declaration must not specify different Base Classes
Solution 1
You need to inherit from ContentPage in the .cs when you use it as Rootelement in the XAML file.
The other problem is, that you need to assign your "viewLayout" to Content instead of View.
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;
}
}
}
Solution 2
As a late entry...
In case you were trying to create a base class for your pages...but it failed. You CAN inherit from a your own custom base class.
Here's how...
1: CREATE A BASE CLASS PAGE (w/ a Code Behind Class):
For example, here is mine, but yours could be different...
<?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: REFERENCE THE BASE CLASS PAGE in your PAGE:
Make sure you reference your views in "xmlns:views". Take special care to notice the pages root element: "<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>
Solution 3
You are mixing up two distinct things here: a page and a view cell.
It is possible for both pages and view cells to be created both through code and using XAML, but they are separate things.
When creating any component in XAML, be it page or view, the root node type must be the type that you are subclassing. The code behind .cs file then has to derive from that same base class, or you can actually leave out the base class completely in the codebehind, as partial classes do not have to re-state what class they are deriving from.
So, for your CashAdvance page definition, definitely remove the ": ViewCell" part from the class definition in the codebehind.
Then you should probably build out your Page in XAML (otherwise, what is the point of using a xaml page anyway, if you build it out in code?)
If you actually need a custom viewcell (for eg. for use inside a ListView) then create another .xaml file for your viewcell, and build out it's own ui there. You can then reference it inside the page XAML or from codebehind.
For more details on using XAML, please take a look at the Xamarin XAML documentation
Related videos on Youtube
Jaycee Evangelista
Updated on June 04, 2022Comments
-
Jaycee Evangelista about 2 years
I repeatedly get this Error Message:Partial Declaration must not specify different Base Classes. Can somebody tell me what may be the cause of this. Here's my code.
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; } } }
-
Akash Amin about 8 yearsWhy are you inheriting viewcell in the .cs page. I think it must be content page. The class you are inheriting must be the root element in your xaml.
-
Jaycee Evangelista about 8 years@Akash Amin I just saw that on the internet, originally it was a ContentPage. But when I'm using a ContentPage, an error will appear stating 'Xamarin.Forms.View' is a 'type' but is used like a 'variable'.
-
Akash Amin about 8 yearsYou can use Content=viewLayout instead of View=viewLayout
-
Jaycee Evangelista about 8 yearsThat seems to remove the error. But another error appear Sir. Stating that "The name 'Fonts' does not exist in the current context". What could be the problem?
-
Akash Amin about 8 yearsThe problem is in "Font = Fonts.Twitter". I don't know what are you using.
-
Jaycee Evangelista about 8 yearsWhat should it be instead Sir?
-
Akash Amin about 8 yearsYou need to use FontFamily to set a font. Fonts.Twitter means you need to have Fonts Class in you application. If not set it to the default font available.
-
Jaycee Evangelista about 8 yearsI have figured it out Sir. Using this code. FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) Thank you so much.
-
Akash Amin about 8 yearsYou can give an upvote if I have helped you, it will be appreciated.
-
Jaycee Evangelista about 8 yearsI tried earlier but I cannot vote for my own post. Is there any other way sir?
-
-
irreal about 8 yearsWhile both are technically issues, and are true, I think it's worth pointing out that there is no point in HAVING a XAML file if you then go ahead and instantiate all the controls in the codebehind and change the entire content property. :)
-
Jaycee Evangelista about 8 years@Marius Junak that seems to solve my problem Sir. Thanks a lot.
-
Jaycee Evangelista about 8 yearsThanks for the tip Sir :)