xamarin 为什么Visual Studio生成带有错误的代码?

jutyujz0  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(144)

每当我试图使用模拟器VS自动生成一段代码,其中有一个错误,是阻止应用程序的工作

namespace MyApp {

[global::Xamarin.Forms.Xaml.XamlFilePathAttribute("NotePage.xaml")]
public partial class NotePage : global::Xamarin.Forms.ContentPage {
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private global::Xamarin.Forms.ContentPage NotePage;
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "2.0.0.0")]
    private void InitializeComponent() {
        global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(NotePage));
        NotePage = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.ContentPage>(this, "NotePage");
    }
}

它会在第一个NotePage下显示错误,错误代码为CS0542,内容为“NotePage:成员名称不能与其封闭类型””相同.
这是主XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:mmvm="clr-namespace:MvvmHelpers;assembly=MvvmHelpers"
         xmlns:viewmodels="clr-namespace:MyApp.ViewModels"
         x:Class="MyApp.NotePage"
         BackgroundColor="White"
         x:Name="NotePage">

<ContentPage.BindingContext>
    <viewmodels:NoteViewModel/>
</ContentPage.BindingContext>

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Add" Command="{Binding AddCommand}"/>
</ContentPage.ToolbarItems>

<ListView
    BackgroundColor="Transparent"
    CachingStrategy="RecycleElement"
    HasUnevenRows="True"
    IsPullToRefreshEnabled="True"
    IsRefreshing="{Binding IsBusy, Mode=OneTime}"
    ItemsSource="{Binding Note}"
    RefreshCommand="{Binding RefreshCommand}"
    RefreshControlColor="DarkViolet"
    SelectionMode="None"
    SeparatorVisibility="None">
    
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="viewmodels:NoteViewModel">
        <ViewCell>
            <ViewCell.ContextActions>
                <MenuItem
                            Command="{Binding Source={x:Reference NotePage}, Path=BindingContext.RemoveCommand}"
                            CommandParameter="{Binding .}"
                            IsDestructive="True"
                            Text="Delete" />
            </ViewCell.ContextActions>
            <Grid Padding="10">
                <Frame CornerRadius="20" HasShadow="True">
                    <StackLayout Orientation="Horizontal">
                        <StackLayout VerticalOptions="Center">
                            <Label
                             FontSize="Large"
                             Text="{Binding Title}"
                             VerticalOptions="Center"/>
                            <Label
                             FontSize="Large"
                             Text="{Binding Content}"
                             VerticalOptions="Center"/>
                        </StackLayout>
                    </StackLayout>
                </Frame>
            </Grid>
        </ViewCell>
    </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我不能把initialize组件放在this下面,因为它说它不存在于当前上下文中:

public partial class NotePage : ContentPage
    {
        public NotePage()
        {
            InitializeComponent();
        }
    }
ubof19bj

ubof19bj1#

您已

x:Name="NotePage"

x:Class="MyApp.NotePage"

这是一个问题,因为它试图创建一个名为NotePage的成员变量,但类名也是NotePage。这正是CS0542告诉你的,“成员名不能与其封闭类型相同”

相关问题