AJ Venturella
AJ Venturella

Reputation: 4902

Error CS0103: The name 'InitializeComponent' does not exist in the current context

Using Visual Studio Community for Mac 7.3.3 (build 7) I make an iOS project. Then make a new .NET Standard 2.0 lib.

Using the built in NuGet support I add Xamarin.Formsto each Project.

In the Net Standard 2.0 Lib I Add -> New File and Select Forms ContentPage Xaml

This creates an empty class and corresponding XAML file. In the classes constructor it contains 1 line:

InitializeComponent();

When I attempt to compile either the NET Standard 2.0 lib OR the iOS project, the compilation fails with the following error:

Error CS0103: The name 'InitializeComponent' does not exist in the current context (CS0103)

I have added no other code or dependencies to either project. Clearly I have missed something that is not obvious to me.

Upvotes: 3

Views: 14937

Answers (3)

HungVu
HungVu

Reputation: 21

I think that you have moved your code to different folder. You can delete folder .vs in your project (this folder is hide) and reopen your project.

Upvotes: 0

hvaughan3
hvaughan3

Reputation: 11105

As DeveloperX said in the comments, open the property window on your XAML file and make sure Build Action = Embedded Resource and the make sure Custom Tool = MSBuild:UpdateDesignTimeXaml.

Finally, to fix a .NET Standard bug in VS add the following to your .NET Standard .csproj file

<ItemGroup>
  <!-- https://bugzilla.xamarin.com/show_bug.cgi?id=55591 -->
  <None Remove="**\*.xaml" />

  <Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
</ItemGroup>

If you are still having problems, then it may be an issue with how the .NET Standard project was setup. Check out the following article which talks about converting a PCL to .NET Standard, which is what we did.

Link

Upvotes: 7

Phillip Wells
Phillip Wells

Reputation: 119

Close any open code behinds or open xaml files.

Upvotes: 0

Related Questions