我在我的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

Context:

项目 struct :

名为"BeautyFly"的Xamarin.Forms项目

App.xaml.cs文件包含App类定义

应用程序主页的MainPage.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类及其引用

Questions:

是什么原因导致我的App.xaml.cs文件中出现"‘App’的部分声明不能指定不同的基类"错误?

如何解决"当前上下文中不存在主页面名称"的错误?

我的代码中是否有需要判断的特定区域来确定和解决此问题?

如有任何指导或建议,我们将不胜感激.谢谢!

推荐答案

看起来你已经换掉了你App.xaml里的东西.

您的App.xaml‘S根元素应该是<Application>.不能指定不同的基类型,这意味着App的基类在XAML和代码隐藏中都必须为Application.

App.xamlApp.xaml.cs是你的应用程序的入口点,没有自己的用户界面.相反,您需要将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,也可以将其包装在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既不是强制性的,也不是常规的,您可以为它 Select 任何合适的名称.

如果您想使用Shell,您可以像您已经拥有的那样使用它,但是您需要向Shell注册您的主页类(参见链接).

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.xamlApp.xaml.cs需要指定相同的基类(=Application),因为它们是同一个类的两个部分.

Csharp相关问答推荐

在Microsoft XNA框架(MonoGame)中旋转相机

C# Json重新初始化动态类型

为什么我在PuppeteerSharp中运行StealthPlugin时会出现错误?

MongoDB实体框架核心:表达必须可写

使用其可能实现的基类和接口的属性的方法

EF Core判断是否应用了AsSplitQuery()

使用特定格式的JsonConvert序列化对象

不仅仅是一个简单的自定义按钮

如何注销Microsoft帐户?

Blazor-从数据库内部服务器提取错误

如何在实体框架中添加包含列表?

如何在C#中使用Postman中的本地IP向本地主机上运行的本地API发出请求

在使用Audit.NET的AuditTrail实现中,如何逐月将数据摄取到AzureTableStorage?

CA1508:';NULL=>;TRUE;始终为';TRUE';.移除或重构条件(S)以避免死代码

如何防止Visual Studio断点以红色突出显示到整行?

为什么我不能从我的异步任务方法中返回异步任务方法?

Azure Functions v4中的Serilog控制台主题

如何从原始图像到新创建的图像获得相同的特定 colored颜色 ,并且具有相同的 colored颜色 量和相同的宽度和高度?

SignalR跨域

无法对包含字符串的列进行排序.请与实体框架联接