问题描述
我刚刚更新了应用程序中的软件包,但遇到了许多我不理解的此类新错误:
I just updated packages in my application and I am getting many new errors of this type that I don't understand:
/Users/alan/Downloads/Japanese 31/Japanese/obj/Debug/Views/Help/GettingStarted.xaml.g.cs(51,51):
Error CS0542: 'GettingStarted': member names cannot be the same as their enclosing type (CS0542) (Japanese)
我有如下所示的XAML代码;
I have XAML code that looks like this;
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.GettingStarted"
x:Name="GettingStarted"
Title="Getting Started">
<ContentPage.Content>
我的C#
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Japanese
{
public partial class GettingStarted : ContentPage
{
public GettingStarted()
{
我编码XAML和CS错误吗?我认为这是使用部分类和相同类名的方法.
Am I coding my XAML and cs wrongly? I thought this is the way to do this with partial classes and the same class name.
推荐答案
x:Name="GettingStarted"
这是问题所在.这将是页面的名称,但它也是该页面的类的名称.为x:Name
属性指定一个值会在您的代码后面创建一个具有该名称的变量.因此,您将获得:GettingStarted GettingStarted = new GettingStarted();
至少可以说这令人困惑.我猜Xamarin团队遇到了这些相同名称和类型的问题,并决定将其阻止.
x:Name="GettingStarted"
this is the problem. This will be the name of your page, but it is also the name of the class of that page. Specifying a value for the x:Name
attribute creates a variable with that name in your code-behind. So what you'll get is: GettingStarted GettingStarted = new GettingStarted();
This is confusing to say the least. I'm guessing the Xamarin team ran into a problem with these identical names and types and decided to block it.
请尝试使用其他名称,或者,如果您不想使用其他大小写,例如:x:Name="gettingStarted"
Try coming up with a different name, or, if you don't want to use a different casing like: x:Name="gettingStarted"
这篇关于XAML/cs-成员名称在更新包后不能与其封闭类型(CS0542)相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!