Search code examples
c#.netmaui

InitializeComponent( ) does not exist


I just open my framework after only 2 days of not touching it, and when I tried to run it and it returns error as shown below.

EditContactPage.xaml.cs Picture

Coding EditContactPage.xaml.cs:

using Contacts.Maui.Models;
using Contact = Contacts.Maui.Models.Contact;

namespace Contacts.Maui.Views;

[QueryProperty(nameof(ContactId), "Id")]

public partial class EditContactPage : ContentPage
{
    private Contact contact;

    public EditContactPage()
    {
        InitializeComponent();
    }

    //  Navigate back to main page.
    //  void btnCancel_Clicked(Object sender, EventArgs e)
    //  {
    //       Shell.Current.GoToAsync($"//{nameof(ContactsPage)}");
    //       Shell.Current.GoToAsync("..");
    //   }

    // property
    public string ContactId
    {
        // value of ContactId
        set
        {
            contact = ContactRepository.GetContactById(int.Parse(value));
            //lblName.Text = contact.Name;
        }
    }
}

I remembered the last time I updated this project, it was still working fine. Am I missing something? I just do not understand how to wrapped my head around the InitializeComponent() function and where could I find its origin.

Edited: included EditContactPage.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Contacts.Maui.Views.EditContactPage"
                 Title="Edit Contact">
        <!--<VerticalStackLayout>
            <Label
                x:Name="lblName"
                Text="Welcome to .NET MAUI!"
                VerticalOptions="Center" 
                HorizontalOptions="Center" />

            <Button x:Name="btnCancel" Text="Cancel" Clicked="btnCancel_Clicked" ></Button>
        </VerticalStackLayout>-->

        <VerticalStackLayout Spacing="10" Margin="10,10,10,0">

            <Border Padding="10,0,0,0" HeightRequest="60">
                <HorizontalStackLayout>
                    <Label Text="Name" VerticalOptions="Center" WidthRequest="50"></Label>
                    <Entry
                        VerticalOptions="Center"
                        HeightRequest="10"
                        WidthRequest="200">
                    </Entry>
                </HorizontalStackLayout>
            </Border>

            <Border Padding="10,0,0,0" HeightRequest="60">
                <HorizontalStackLayout>
                    <Label Text="Email" VerticalOptions="Center" WidthRequest="50"></Label>
                    <Entry
                        VerticalOptions="Center"
                        HeightRequest="10"
                        WidthRequest="200">
                    </Entry>
                </HorizontalStackLayout>
            </Border>

            <Border Padding="10,0,0,0" HeightRequest="60">
                <HorizontalStackLayout>
                    <Label Text="Phone" VerticalOptions="Center" WidthRequest="50"></Label>
                    <Entry
                        VerticalOptions="Center"
                        HeightRequest="10"
                        WidthRequest="200">
                    </Entry>
                </HorizontalStackLayout>
            </Border>

            <Border Padding="10,0,0,0" HeightRequest="60">
                <HorizontalStackLayout>
                    <Label Text="Address" VerticalOptions="Center" WidthRequest="50"></Label>
                    <Entry
                        VerticalOptions="Center"
                        HeightRequest="10"
                        WidthRequest="200">
                    </Entry>
                </HorizontalStackLayout>
            </Border>

        </VerticalStackLayout>

    </ContentPage>


Solution

  • As ToolmakerSteve said that:

    Best case, you have a recent source backup. Download source to a new folder, see if build works there.

    If it doesn't work, then you can try Jason’s suggestion(clean solution and/or delete your bin/obj folders).

    For more things about this error, you can see this issue on GitHub: CS0103 The name 'InitializeComponent' does not exist in the current context.