Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms - InitializeComponent doesn't exist when creating a new page

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 that The 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();         }     } 
like image 792
user2915962 Avatar asked Mar 02 '15 20:03

user2915962


1 Answers

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:

Screenshot UPDATE:

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.

like image 67
Dushyant Bangal Avatar answered Sep 21 '22 18:09

Dushyant Bangal