Reputation: 577
So suddenly the common error "The name InitializeComponent does not exist in the current context" appeared in every single xaml.cs class in all my solution, I have tried multiple common solutions to this issue but none of them seems to work.
A xaml.cs inheriting a Content view:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace FirstApp.Renderers
{
public partial class CustomExpansibleMenu : ContentView
{
public CustomExpansibleMenu()
{
//Error here
InitializeComponent();
}
}
}
A xaml.cs inheriting from a Content Page:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FirstApp.Services;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace FirstApp.Pages
{
public partial class ChatMenuPage : ContentPage
{
private NavigationService navigation;
public static double numPixelsW;
public static double numPixelsH;
public ChatMenuPage()
{
numPixelsH = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;
numPixelsW = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;
navigation = new NavigationService();
//Error here
InitializeComponent();
//Another error here, searchBox is the name of a frame in my xaml page, but //I cant access to it because of the previous error
searchBox.BorderWith = 3;
}
}
}
These are the common solutions that I have tried:
Upvotes: 4
Views: 13508
Reputation: 11
If you have a command prompt open to \bin\debug\net48
you need to cd
out of that directory so the error goes away.
Upvotes: 1
Reputation: 21
This can be caused by installing a wrong package. If you have done so, removing it will solve the problem.
Upvotes: 2
Reputation: 131
Delete bin and obj files in the project directory and then rebuild.
Upvotes: 7
Reputation: 1
Just in case any WPF-Avalonia developer sees this comment. To solve this bug:
For e.g, if you want to make a UserControl
project:
.csproj
UserControl
by Avalonia UserControl template.Upvotes: 0
Reputation: 268
I was also having this issue after pulling a bunch of functionality out into a library. I deleted .vs, bin, and obj folders in all projects. That let me build. Then to get Intellisense working again had to modify the element's name in the XAML, save, and revert the changes.
Upvotes: 1
Reputation: 769
I got that error because I had changed Namespace and renamed the files.
In my cause I came to know about this very late, I had created a new project with the same files.
Upvotes: 0
Reputation: 11
in xaml is not pointing to the correct xaml.cs class of codebehind for example:
MyView.xaml must be pointing to MyView.xaml.cs on content page like
Upvotes: 1
Reputation: 193
I see this a lot in my WPF projects. IntelliSense shows the 'error' "The name 'InitializeComponent' does not exist in the current context", with the same error (CS0103) on every object defined in my .xaml file. Yet the project builds and runs without problems. If I hover over InitializeComponent(), select "Show potential fixes", and accept the suggestion "Generate method 'MainWindow.InitializeComponent'", then the IntelliSense 'error' (red squiggle) disappears. But when I try to build my project, I get the build error "CS0111 Type 'MainWindow' already defines a member called 'InitializeComponent' with the same parameter types". @Jason 's suggestion sometimes works, but not always. Since the errors are not 'real', and do not affect the build, I haven't investigated any deeper. It would be nice if this bug could get fixed.
Upvotes: 1
Reputation: 577
After 1 year and 4 months, I have found that this is a ghost error, that is, the problem is something else that ends up showing to us as the one discussed in this post. So, what and where is the actual error and how do we solve it? Well... bad news, it can be caused by many, MANY errors. These are the ones I have faced, although there are more (I won't include them since I haven't experienced them).
Bug: First of all verify that this is not a bug, to do this make a small change in the XAML file and save it (a space + save is enough), if the problem is solved congratulations, if not keep reading and good luck...
Namespace of the XAML.cs file and x:class of XAML file aren't matching: This is by far the most common mistake and the only scenario I have seen where the error doesn't show as a ghost error. Check all your Pages and change the namespace or x:class if they don't match.
Build Action or Custom Tool of a Page set incorrectly: In this case there is another easily identifiable problem. To check if these 2 properties are set correctly right click on the page, go to properties, and verify that Build Action is set to EmbeddedResource, and that Custom Tool is set to MSBuild:UpdateDesignTimeXaml.
Workaround: If the previous solutions didn't work for you things will probably get complicated, that is why before continuing to the following solutions make sure you do this: 1) delete all files inside the bin & obj folders of your repository, 2) Clean project + Rebuild, 3) Restart your computer. Sometimes this solves it (I don't know why), and don't worry, these files aren't important.
Common .csproj is hiding or removing XAML or XAML.cs files: God knows why sometimes " <Folder delete" is added to your common .csproj file (it might be something similar to delete, such as hide or remove), removing some of your XAML and XAML.cs files from the solution (don't worry, the files are fine). To solve this simply remove these lines.
Found conflicts between different versions of the same dependent assembly: In this scenario you might have this as a warning or as an error. To solve this I added some lines to my ".csproj" file. It will make you reload your project, and once this happens you can remove it (you might even be forced to do so). I don't recommend doing this unless you are 100% sure this is happening to you, the reason being that I don't know what this does, I found it months ago on a blog:
<Target Name="GetDependencyTargetPaths" AfterTargets="ResolvePackageDependenciesForBuild">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="@(ResolvedCompileFileDefinitions)" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>
<PackageReference Include="System.Reflection.Emit">
<Version>4.3.0</Version>
<ExcludeAssets>All</ExcludeAssets>
<IncludeAssets>none</IncludeAssets>
</PackageReference>
As I said, this is a ghost error, it can happen by several conflicts between libraries, by Visual Studio randomly changing properties of your files, and so on, hence, none of these solutions might work for you. If you solved it in any other way please add it in this post, it will be extremely helpful to everyone.
If you still have this problem you can find more information on other posts, so go ahead and look for them, you will eventually find a solution, good luck. (:
Upvotes: 12