Search code examples
c#xaml

CS0103, 'InitializeComponent' doesn't exist in current context when working with other XAML pages


I have been stuck on this for hours, the 'InitializeComponent' doesn't exist in current context.

public partial class CreateAccount : Window
{
    public CreateAccount()
    {
        InitializeComponent();
    }

Here is my XAML code

<Window x:Class="HotelAccomadation.Interface.CreateAccount"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:HotelAccomadation.Interface"
    mc:Ignorable="d"
    Title="CreateAccount" Height="450" Width="800"
    Background="AliceBlue"
    >

I have made sure the names are the same, what else should I do?


Solution

  • The solution to your issue is internal to your solution file, and isn't something immediately obvious. Follow these steps and you will be able to build your solution again.

    1. Open your solution in Visual Studio

    2. Right Click the project file (directly under the "Solution 'HotelAccommadation' (1 of 1 Project)" entry in the Solution Explorer

    3. Select "Open folder in File Explorer"

    4. Find the file "HotelAccommadation.csproj"

    5. Edit this file so it looks like the code below I have attached. Be careful to match this exactly.

    6. Clean your solution (from Solution Explorer)

    7. Build your solution

    Make sure you delete old classes referenced in this file, and make sure you add the new classes carefully.

    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
        <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
        <EnableDefaultPageItems>false</EnableDefaultPageItems>
    </PropertyGroup>
    
    <ItemGroup>
        <EmbeddedResource Remove="obj\**" />
        <None Remove="obj\**" />
    </ItemGroup>
    
    <ItemGroup>
        <None Remove="Interface\Booking.xaml" />
        <None Remove="Interface\CreateAccount.xaml" />
        <None Remove="Interface\Login.xaml" />
        <None Remove="Interface\Searchpg.xaml" />
        <None Remove="Interface\CreateAccommodation.xaml" />
    </ItemGroup>
    
    <ItemGroup>
        <Compile Include="DAL\AccessData.cs" />
        <Compile Include="Interface\Booking.xaml.cs" />
        <Compile Include="Interface\CreateAccommodation.xaml.cs" />
        <Compile Include="Interface\CreateAccount.xaml.cs" />
        <Compile Include="Interface\Searchpg.xaml.cs" />
        <Compile Include="Logic\createAccount.cs" />
        <Compile Include="Logic\LoginLg.cs" />
        <Compile Include="Interface\Login.xaml.cs" />
        <Compile Include="Logic\SearchLg.cs" />
        <Compile Include="Models\AccomMod.cs" />
        <Compile Include="Models\BookingMod.cs" />
        <Compile Include="Models\userModel.cs" />
    </ItemGroup>
    
    <ItemGroup>
        <PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
    </ItemGroup>
    
    <ItemGroup>
        <Page Include="Interface\Booking.xaml">
            <Generator>MSBuild:Compile</Generator>
        </Page>
        <Page Include="Interface\CreateAccount.xaml">
            <Generator>MSBuild:Compile</Generator>
        </Page>
        <Page Include="Interface\Login.xaml" />
        <Page Include="Interface\Searchpg.xaml">
            <Generator>MSBuild:Compile</Generator>
        </Page>
        <Page Include="Interface\CreateAccommodation.xaml">
            <Generator>MSBuild:Compile</Generator>
        </Page>
    </ItemGroup>