我的 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
背景:
项目结构:
名为“BeautyFly”的 Xamarin.Forms 项目
App.xaml.cs 文件包含 App 类定义
应用程序主页的 MainPage.xaml 和 MainPage.xaml.cs 文件
代码片段:
应用程序.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 文件中出现“‘App’的部分声明不得指定不同的基类”错误?
如何解决“当前上下文中不存在名称‘MainPage’”错误?
我的代码是否有任何特定区域需要检查以识别和解决此问题?
任何指导或建议将不胜感激。谢谢!
您似乎已替换了 App.xaml 的内容。
您的 App.xaml 的根元素应该是
<Application>
。您无法指定不同的基类型,这意味着 App
的基类在 XAML 和代码隐藏中都必须是 Application
。
App.xaml 和 App.xaml.cs 是应用程序的入口点,没有自己的 UI。相反,您需要将
MainPage
类的 App
属性设置为 NavigationPage、ContentPage 或 AppShell。
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.xaml 和 MainPage.xaml.cs(只需将一个新页面添加到您的项目并在向导中将其称为 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());
}
}
}
如果您想使用 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
),因为它们是同一类的两个部分。