简体   繁体   中英

The name 'InitializeComponent' does not exist in the current context when creating new xaml file

I have searched for an answer to this question for a while so I hope that my instance can be solved. I created a new Xamarin.Forms shared projects in visual studio 2015 community. I created a new xaml file and name it and placed it in a folder named Pages in the shared app project. ,并将其放置在共享应用程序项目中名为Pages的文件夹中。 After adding the Xamarin.Forms namespace for it to recognize and removing 并将其删除 before I have this: 之前,我有这个:

using Xamarin.Forms;

namespace BeneFit.Pages
{

    public sealed partial class LoginPage : Page
    {
        public LoginPage()
        {
        InitializeComponent(); //This has the red squiggly
        }
    }
}

The Xaml file here has not been changed at all:

<Page
    x:Class="BeneFit.Pages.LoginPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BeneFit.Pages"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    </Grid>
</Page>

One thing I have notice a lot is that the build action needs to be which I made sure of in this case. Also the suggested quick fix was to add this:

    private void InitializeComponent()
    {
        throw new NotImplementedException();
    }

which doesnt really solve the issue because I know I should have to do it. Any help or suggestions would be huge, ive been trying to deal with this for a while.

When you create a new control or page in XAML, a hidden partial class file is also generated behind the scenes, this holds your InitializeComponent() method. In the case of a page called LoginPage the generated code behind will be called LoginPage.gics , and it should reside in the folder structures under the obj folder.

Your XAML line

xmlns:local="using:BeneFit.Pages"

does look a bit funky, I would change that to

xmlns:local="clr-namespace:BeneFit.Pages"

Go to each project folder (shared, .Android, .ios etc) and delete obj and bin folders (this will remove the hidden partial .g file)

Rebuild the solution.

Three checks to fulfill:

1 - Latest Xamarin Forms nuget installed same versions for all platforms

2 - For page.Xaml properties are :

   Build Action: Embedded Resource
   Copy to output dir: Do not copy
   Custom tool (most common error when created page manually): MSBuild:UpdateDesignTimeXaml

For page.cs properties are:

Build action: Compile
Copy to output dir: Do not copy

3 - Right-click project - Unload project, then reload project.

That looks like UWP XAML not Xamarin Forms XAML.

Are you creating the wrong new thing?

You should be creating a new Xamarin.Forms Content Page

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM