Xamarin.Forms - 创建新页面时,InitializeComponent不存在

75

我正在使用Visual Studio尝试使用Xamarin.Forms。我正在尝试按照指南操作:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getting_started_with_xaml/

简而言之,我创建了一个Xamarin.Forms解决方案,使用PCL,然后尝试将Forms XAML Page添加到PCL项目中。

自动生成的代码如下:

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent(); 
        }
    }

这里的问题是InitializeComponent();变成红色了。当我尝试构建时,我收到了The name 'InitializeComponent' does not exist in the current context的通知。

我一直在寻找解决方案,尽管其他人也遇到了同样的问题,但他们的解决方案对我无效。下面是一个建议我尝试使用过的链接: http://blog.falafel.com/xamarin-error-initializecomponent-does-not-exist-in-the-current-context/

如果您有解决这个问题的方案,请告诉我。谢谢!

更新:

我的PCL(我也想在其中添加XAML页面)包含:

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
        }
    }

我的 XAML 页面:

<?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>

代码后端:

    public partial class MyXamlPage : ContentPage
    {
        public MyXamlPage()
        {
            InitializeComponent();
        }
    }

你使用的 Xamarin Forms 项目版本是哪个?我记得旧的项目模板存在问题。 - Steve Mitcham
我应该提到我使用的是Visual Studio和试用版。如何检查版本或更新到最新版本?谢谢您的回答。我尝试右键单击pcl并从nuget下载最新的xamarin-Forms。仍然存在同样的问题。 - user2915962
你能分享一下你的项目源代码吗? - Artur Shamsutdinov
谢谢,我已经更新了问题。如果您需要更多信息,请告诉我。 - user2915962
以防万一:我在将解决方案移动到新文件夹后遇到了相同的问题。我不得不手动编辑.csproj文件,以更新“packages”文件夹,然后它又可以重新编译了。 - bN_
34个回答

3

如果您在Xamarin.Forms页面中遇到Intellisense错误(如InitializeComponent),但实际上项目可以成功构建和运行,则只需使用向导添加一个新的Forms页面即可解决所有错误...

然后,您可以删除该新页面。


2

请仔细检查类名。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="{AppName}.{ClassName}">
</ContentPage>

类名应该由应用程序名称和部分类名组合而成。

2

对我来说,清理并重新构建解决了问题!


2

我用的非常简单的方法:

  • 复制xaml/cs文件的内容
  • 删除xaml/cs文件
  • 创建一个新类并粘贴内容

现在,InitializeComponent()函数不再有红色下划线。

希望这能帮到某些人。


是的,我也发现了。什么都没用。最终,复制内容,完全删除文件并从头重新添加它们是唯一的解决方案。 - rob_james

2
  • 我在 Visual Studio 2013 升级4环境中遇到了类似的问题,尝试了网上所有推荐的解决方法,但都没有解决我的问题。
  • 然后我尝试了一种变通的方法。我安装了 Visual Studio 2015 预览版,并创建了一个带有xamarin表单项目的新空白应用程序。
  • 当我添加新的 Xaml 文件时,一切都正常,InitialComponent 方法的问题消失了。
  • 我不知道问题的确切原因,但似乎与配置设置有关。

欢迎来到 Stackoverflow。使用一些格式化使其易读。 - Nagama Inamdar
我也不得不这样做。但是当我打开在VS 2013中创建的旧项目时,错误又出现了!在VS 2015中创建的项目没有显示它。也许这可以缩小原因。 - Dushyant Bangal

2

右键点击 *.xaml 文件,然后选择属性,在 "自定义工具" 中输入 "MSBuild:UpdateDesignTimeXaml",接着将 "生成操作" 改为 "嵌入的资源",然后重新构建项目即可。


1
将页面属性更改为:

BuildAction => Embedded resource 
CustomTools => MSBuild:UpdateDesignTimeXaml

1
当解决方案的项目引用了版本为1.4.0.0的dll“Xamarin.Forms.Core”,“Xamarin.Forms.Xaml”和“Xamarin.Forms.Platform” 1.0.0.0时,会出现此问题。
为了解决它,我不得不降级到版本为1.3.3.0的dll,但使用此版本时,Xamarin.Forms.Platform 1.0.0.0不存在。

1

我刚刚更新了所有的包,运行正常。


1

看起来这是由许多原因引起的,所以如果你已经阅读了所有这些内容但还没有解决:

我的问题是由于解决方案配置被设置为发布。将其更改为调试即可解决错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接