Xamarin.Forms - InitializeComponent doesn't exist when creating a new page
Solution 1
UPDATE: This error doesn't usually appear in VS 2015, if it does, here's my original answer:
Found the solution! Right click on the .XAML file, select Properties.
You will see a Property called Custom Tool. Change its value from MSBuild:Compile
to MSBuild:UpdateDesignTimeXaml
This will solve the problem. Dont know about the downvote, but here's my screenshot:
It reappears rarely. If it does, just open the Xaml and code behind files and save them. I know, its not the best solution, but it gets the job done.
Solution 2
I get this sometimes and here's the checklist that solved them so far:
Make sure the namespace in .xaml and .xaml.cs match
Inherit from the correct parent - ContentPage for a page and ContentView for a control
Set build action of the .xaml file to
Embedded Resource
if in the shared project.
Solution 3
As far as my observation is concerned, in Visual Studio 2015, XAML properties are already set as suggested by highly-voted answers here by default, specifically :
- Custom Tool =
MSBuild:UpdateDesignTimeXaml
- Build Action =
Embedded Resource
but the error still appears sometimes... (like in this other question).
Editing the corresponding XAML file and then hit CTRL+S should work fine, but you don't have to. A cleaner way to force Custom Tools to be run is by right-clicking on the XAML file and then click on "Run Custom Tool" context menu.
Solution 4
Updating the Xamarin.Forms NuGet package should do the job
Solution 5
I have met this problem. It's associated with the encoding of XAML files in VS. I'm using VS2015.
I solved this problem as follows:
Open the *.xaml file in the project and click Save button. (There will be applying the correct encoding in VS2015).
Then reopen the project and rebuild it. Now there are no errors.
user2915962
Updated on July 05, 2022Comments
-
user2915962 about 2 years
I'm using Visual Studio to try out Xamarin.Forms. I'm trying to follow the guide: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getting_started_with_xaml/
In short, I create a Xamarin.Forms solution using a PCL and then try to add a
Forms XAML Page
to the PCL-project.The code-behind that gets created looks like this:
public partial class Page1 : ContentPage { public Page1() { InitializeComponent(); } }
The problem here is that
InitializeComponent();
is red. When I try to build I get informed thatThe name 'InitializeComponent' does not exist in the current context
I've been looking around for solutions and even though others have had the same trouble, their solutions wont work for me. Here is one suggestion i tried to use: http://blog.falafel.com/xamarin-error-initializecomponent-does-not-exist-in-the-current-context/
Please let me know if you have a solution for this problem. Thanks!
Update:
My PCL (which is where I also want to add my XAML-page) contains:
App.cs:
public class App : Application { public App() { // The root page of your application MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { new Label { XAlign = TextAlignment.Center, Text = "Welcome to Xamarin Forms!" } } } }; } protected override void OnStart() { // Handle when your app starts } protected override void OnSleep() { // Handle when your app sleeps } protected override void OnResume() { // Handle when your app resumes } }
And my XAML-page:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="XamaTest.MyXamlPage"> <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" /> </ContentPage>
Code-behind:
public partial class MyXamlPage : ContentPage { public MyXamlPage() { InitializeComponent(); } }
-
user2915962 over 9 yearsThank you for the tip. Unfortunatly it did not change anything in my case.
-
Thomas Weller over 9 yearsHmm. I had the same problem and it worked for me. Are there any other packages in your solution?
-
user2915962 over 9 yearsNo, just a "fresh" Xamarin.Forms app with the default PCL.
-
user2915962 over 9 yearsFound the solution here: blog.galasoft.ch/posts/2014/07/… Appearatly there is a bug when you create a new xaml-page. It automatically gets the extension .cs instead of xaml. I never noticed this. Thank you!
-
Nagama Inamdar over 9 yearsWelcome to Stackoverflow. Use some formatting to make it readable format.
-
Richard Logwood almost 9 years@user2915962 The NuGet package update and clean/rebuild (+restarts) did not fix the problem for me. After reading you comment, I decided to add a new Xamarin Forms XAML page. The "InitializeComponent" error on both pages went away after a build with the new page! Looks like a VS state issue of some kind. When the problem was there I could step through the compiler generated InitializeComponent code without a problem. I'm guessing that adding a new XamForms XAML page after the NuGet package upgrade helped sort out some VS internal state that a clean/rebuild did not, but that is unconfirmed.
-
Dushyant Bangal over 8 yearsI had to do that too. But when I opened the old project made in VS 2013, the error came back! Projects created in VS 2015 don't show it. Maybe that will narrow the cause.
-
David over 8 yearsI had 1 element/control that didn't have x:Name attribute on it..once i added that the error went away
-
vamyip over 8 yearsWorked for me, Thanks!!
-
Dushyant Bangal over 8 yearsGood to know. You can mark it "useful" for the next guy by giving +1
-
Tony Trembath-Drake over 8 yearsand for me - i needed 4. Property called Custom Tool. Change its value from MSBuild:Compile to MSBuild:UpdateDesignTimeXaml as said in another post here...
-
Dalbergia about 8 yearsSetting the build action to EmbeddedResource fixed this for me. I copied the .xaml and .xaml.cs files over from a different project, and the .xaml didn't have a build action set. So thanks for your checklist.
-
angularsen about 8 yearsGot the same on a clean new project from template. Changing and re-saving the code-behind and .xaml file fixed it for me.
-
frostymarvelous about 8 yearsMy problem was with x:Class. It seems it was still pointing at Page1
-
Simon K. about 8 yearsThis was set in my case, but the error did appear still.
-
Dushyant Bangal about 8 years@SimonK. have you tried the re-saving trick? I haven't worked on Xamarin for a few months now, but looks like people are still facing this problem, and the trick is working for them too.
-
Simon K. about 8 yearsProject clean and rebuild did it for me.
-
Ismapro about 8 yearsThought it was the solution actually I already had it implemented. I also tried the resaving trick and it did not work.
-
Dushyant Bangal about 8 years@Ismapro , clean and build might help. Its such a sad thing they haven't fixed this yet.
-
jan hruska about 8 yearsIt works for me. Adding bug to blank project it is really amateur work from microsoft. It took me a lot of time to just compile hello world. God bless android studio
-
Aman Sura about 8 yearsMake sure the namespace in .xaml and .xaml.cs match.. This is an important point highlighed. Visual Studio sometimes do not autoupdate this.
-
sh1rts almost 8 yearsThis worked for me too ! Really hope this gets fixed by MS/Xamarin soon..
-
Veverke almost 8 yearsGetting it when trying to run Xamarin's Developer Modal Pages sample. Your solution does not work, the Custom Tool setting by the way is already set to what you mention.
-
penguru over 7 yearsWorked for me as well!
-
Dpedrinha over 7 yearsActually updating the Xamarin.Forms is the main reason this bug happens. I don't know what they are doing but every stable release this bug comes back. Even after pre releases that fixed it. Each time with a different workaround.
-
Bruno Brito about 7 yearsIn my project, the value was MSBuild:UpdateDesignTimeXaml. After i change to MSBuild:Compile its fine. VS SUCKS
-
Dushyant Bangal about 7 years@Veverke, sorry for the late reply, but look at the bottom of my answer. If you already have
MSBuild:Compile
just open the Xaml and code behind files and save them. I know, its not the best solution, but it gets the job done. -
Diego Rafael Souza about 7 yearsRecently I had same issue, and this solution was not solving because of the very long path of my project files. I mean more then 160 chars 'til the class name. The .g.cs was unable to be generated. Changing the path ended the struggle.
-
shalin almost 7 yearsThis answer worked, unload error containing proj and edit .csproj file to have xamarin version you have in package folder
-
Cobus Kruger over 6 yearsFun fact: If you use the rename refactor on the class, the XAML is not updated, leading to this error. The IDE, as ever, is no help.
-
rob_james about 6 yearsYes, I found the same. Nothing worked. Eventually, copying the content, completely deleting the files and re-adding them from scratch was the only solution.
-
MrClan almost 6 yearsyes, this is the major reason. Whenever we change namespace or class name of xaml.cs file, the associated xaml file is not updated. And this results in the mentioned error.
-
Konstantin over 5 yearsI didn't change the encoding. Simple "rebuild" helped in my situation
-
cagri over 5 yearsi had 1 error. now i have 2 errors after doing these steps.
-
HASSAN MD TAREQ over 5 yearsWorked. I renamed
Foo.xaml.cs
but forgot to changex:Class="XXX.Foo"
(refatoring is bad in VS) -
Vering over 5 yearsTHANK YOU!!! Been running around for hours with no working solution. It did the same thing - renamed the xaml.
-
Paul McCarthy almost 5 years"Lets auto generate code and have no link in the project for where it is or came from." What fu@!ing idition thought that this was a good idea.
-
Ryano about 2 yearsWorked for me too. Just had to make sure the x:class matched the real class. Thank you!