Search code examples
c#xamlxamarinxamarin.forms

Error in Xamarin.Forms: "Partial declarations of 'App' must not specify different base classes"


I'm facing an issue in my Xamarin.Forms project, and I'm seeking assistance in resolving it. I've encountered the following error:

Severity Code Description Project File Line Suppression State Error CS0263 Partial declarations of 'App' must not specify different base classes BeautyFly C:\Users\lenovo\Desktop\BeautyFly\BeautyFly\BeautyFly\App.xaml.cs 9 Active

Additionally, there is a related error:

Severity Code Description Project File Line Suppression State Error CS0103 The name 'MainPage' does not exist in the current context BeautyFly C:\Users\lenovo\Desktop\BeautyFly\BeautyFly\BeautyFly\App.xaml.cs 19 Active

Context:

Project Structure:

Xamarin.Forms project named "BeautyFly"

App.xaml.cs file contains the App class definition

MainPage.xaml and MainPage.xaml.cs files for the main page of the application

Code Snippets:

App.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="BeautyFly.App">

    <StackLayout>
        <Entry x:Name="resultEntry" Text="0" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" FontSize="30" HorizontalTextAlignment="End"/>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Text="7" Clicked="OnNumberClicked" Grid.Row="1" Grid.Column="0" />
            <Button Text="8" Clicked="OnNumberClicked" Grid.Row="1" Grid.Column="1" />
            <Button Text="9" Clicked="OnNumberClicked" Grid.Row="1" Grid.Column="2" />
            <Button Text="/" Clicked="OnOperatorClicked" Grid.Row="1" Grid.Column="3" />

            <Button Text="4" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="0" />
            <Button Text="5" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="1" />
            <Button Text="6" Clicked="OnNumberClicked" Grid.Row="2" Grid.Column="2" />
            <Button Text="*" Clicked="OnOperatorClicked" Grid.Row="2" Grid.Column="3" />

            <Button Text="1" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="0" />
            <Button Text="2" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="1" />
            <Button Text="3" Clicked="OnNumberClicked" Grid.Row="3" Grid.Column="2" />
            <Button Text="-" Clicked="OnOperatorClicked" Grid.Row="3" Grid.Column="3" />

            <Button Text="0" Clicked="OnNumberClicked" Grid.Row="4" Grid.Column="0" />
            <Button Text="." Clicked="OnDecimalClicked" Grid.Row="4" Grid.Column="1" />
            <Button Text="=" Clicked="OnEqualClicked" Grid.Row="4" Grid.Column="2" />
            <Button Text="+" Clicked="OnOperatorClicked" Grid.Row="4" Grid.Column="3" />
        </Grid>
    </StackLayout>

</ContentPage>

App.xaml.cs:

using BeautyFly.Services;
using BeautyFly.Views;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace BeautyFly
{
    public partial class App : Application
    {
        private string currentInput = string.Empty;
        private string currentOperator = string.Empty;
        private double result = 0;
        public App()
        {
            InitializeComponent();

            DependencyService.Register<MockDataStore>();
            MainPage = new AppShell();
        }
        private void OnNumberClicked(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            currentInput += button.Text;
            resultEntry.Text = currentInput;
        }

        private void OnOperatorClicked(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            currentOperator = button.Text;
            result = double.Parse(currentInput);
            currentInput = string.Empty;
        }

        private void OnEqualClicked(object sender, EventArgs e)
        {
            double secondOperand = double.Parse(currentInput);

            switch (currentOperator)
            {
                case "+":
                    result += secondOperand;
                    break;
                case "-":
                    result -= secondOperand;
                    break;
                case "*":
                    result *= secondOperand;
                    break;
                case "/":
                    if (secondOperand != 0)
                        result /= secondOperand;
                    else
                        resultEntry.Text = "Error";
                    break;
            }

            resultEntry.Text = result.ToString();
            currentInput = string.Empty;
        }

        private void OnDecimalClicked(object sender, EventArgs e)
        {
            if (!currentInput.Contains("."))
            {
                currentInput += ".";
                resultEntry.Text = currentInput;
            }
        }
    }
}

Troubleshooting Steps Taken:

Checked the inheritance of the App class Verified the MainPage class and its references

Questions:

What could be causing the "Partial declarations of 'App' must not specify different base classes" error in my App.xaml.cs file?

How can I resolve the "The name 'MainPage' does not exist in the current context" error?

Are there any specific areas of my code that I should review to identify and fix this issue?

Any guidance or suggestions would be greatly appreciated. Thank you!


Solution

  • It seems that you've replaced the contents of your App.xaml.

    Your App.xaml's root element should be <Application>. You cannot specify a different base type, meaning that the base class of App must be Application in both XAML and code-behind.

    App.xaml and App.xaml.cs are the entry point of your app and do not have their own UI. Instead, you need to set the MainPage property of the App class to a NavigationPage, ContentPage or AppShell.

    The App class hosts the application and its content and the App.xaml part of it should only hold declarations, like styles and such:

    <?xml version="1.0" encoding="utf-8" ?>
    <Application
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:d="http://xamarin.com/schemas/2014/forms/design"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Class="BeautyFly.App">
      <Application.Resources>
        <ResourceDictionary>
          
          <!-- global converters, styles, etc. go here -->      
          
        </ResourceDictionary>
      </Application.Resources>
    </Application>
    

    Now, you should either have or create a MainPage.xaml and MainPage.xaml.cs (simply add a new ContentPage to your project and call it MainPage in the wizard).

    Then, in your App.xaml.cs you can either set the MainPage class directly as the MainPage of the app or wrap it in a NavigationPage, e.g.:

    using BeautyFly.Services;
    using BeautyFly.Views;
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace BeautyFly
    {
        public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
    
                MainPage = new NavigationPage(new MainPage());
                //or:
                MainPage = new MainPage();
            }
        }
    }
    

    Note the difference between the MainPage property (part of the Application base class) and the MainPage class you need to create, if you haven't already. The name MainPage is not mandatory nor conventional, you can choose any appropriate name for it.

    If you want to use Shell, you do as you already have it, but then you need to register your MainPage class with Shell (see link).

    using BeautyFly.Services;
    using BeautyFly.Views;
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace BeautyFly
    {
        public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
                MainPage = new AppShell();
            }
        }
    }
    

    The important take-away here is that App.xaml and App.xaml.cs need to specify the same base class (= Application), because they're two parts of the same class.