XAML Xamarin.Forms中的错误:“'App'的部分声明不能指定不同的基类”

6ju8rftf  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(115)

我在Xamarin.Forms项目中遇到了一个问题,我正在寻求帮助来解决它。我遇到了以下错误:

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

字符串
此外,还有一个相关的错误:

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

上下文:

项目结构:
Xamarin.名为“BeautyFly”的表单项目
cs文件包含App类定义
xaml和MainPage.xaml.cs文件,用于应用程序的主页
程式码片段:
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;
            }
        }
    }
}


故障排除步骤:
验证App类的继承已验证MainPage类及其引用

问题:

什么可能导致App.xaml.cs文件中出现“Partial declarations of 'App' must not specify different base classes”错误?
如何解决“名称'MainPage'在当前上下文中不存在”错误?
我的代码中是否有任何特定的区域需要检查以识别和修复此问题?
如有任何指导或建议,将不胜感激。谢谢!

9wbgstp7

9wbgstp71#

您似乎已经替换了 * App.xaml * 的内容。
您的 App.xaml 的根元素应为<Application>。您不能指定其他基类型,这意味着App的基类在XAML和代码隐藏中都必须为Application

  • App.xaml* 和 App.xaml.cs 是您应用的入口点,没有自己的UI,需要将App类的MainPage属性设置为 NavigationPageContentPageAppShell

App类承载应用程序及其内容,它的 App.xaml 部分应该只包含声明,如样式等:

<?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>

字符串
现在,您应该拥有或创建一个 MainPage.xamlMainPage.xaml.cs(只需将新的 ContentPage 添加到项目中,并在向导中将其称为 MainPage)。
然后,在您的 App.xaml.cs 中,您可以直接将 MainPage 类设置为应用程序的MainPage,或者将其 Package 在 NavigationPage 中,例如:

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();
        }
    }
}


注意MainPage属性(Application基类的一部分)和你需要创建的MainPage类之间的区别,如果你还没有的话。名称MainPage不是强制性的,也不是常规的,你可以为它选择任何合适的名称。
如果你想使用Shell,你可以按照你已经拥有的那样去做,但是你需要用Shell注册你的MainPage类(见链接)。

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();
        }
    }
}


这里需要注意的是,* App.xaml * 和 * App.xaml.cs * 需要指定相同的基类(= Application),因为它们是同一个类的两部分。

相关问题