Home > Back-end >  Why is Visual Studio generating code with errors?
Why is Visual Studio generating code with errors?

Time:08-11

Whenever I am trying to use the emulator VS auto generates a piece of code which has an error inside it that is blocking the app from working

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

It gives an error under the first NotePage with the error code CS0542, saying "NotePage: member names cannot be the same as their enclosing type".

This is the main 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>

And I can't place initialize component under this, because it says it doesnt exist in the current context:

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

CodePudding user response:

you have

x:Name="NotePage"

and

x:Class="MyApp.NotePage"

this is a problem because it is trying to create a member variable named NotePage but the name of the class is also NotePage. This is exactly what CS0542 is telling you, "member names cannot be the same as their enclosing type"

  • Related