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个回答

1
我偶尔会遇到同样的问题,以下是我解决它的方法:在PCL项目中,我添加了一个新的跨平台XAML页面到项目中。添加新的XAML页面需要几秒钟时间来进行“挂钩”的引用。成功将新的XAML页面添加到项目后,带有问题的XAML页面上的红色下划线将被清除。一旦问题得到解决,我只需删除刚刚添加的XAML文件即可。总之,对我来说,添加一个新的XAML页面然后再删除它一直是解决问题的方法。

1

检查您项目中引用的 Xamarin.Forms 包的版本。

.csproj file Xamarin.Forms nuget package


这个答案有效,卸载包含错误的proj并编辑.csproj文件,将其与包文件夹中的Xamarin版本相匹配。 - shalin

1
我遇到了一个缓存问题,导致出现了这个错误。
解决方法很简单,只需要卸载当前的Xamarin.Forms包,并重新安装之前正常工作的版本。
完成重建后,再次将该包更新至最新版本。

0

最终解决方案是在文本编辑器中编辑项目文件,然后从另一个不冲突的文件中复制模式。这个解决方案对我有效。通常,您会寻找这种语法:

<Compile Update="Your\Path\YourContentPage.xaml.cs">
  <DependentUpon>YourContentPage.xaml</DependentUpon>
</Compile>

0

我在根元素中缺少一个特定的引用属性(在我的情况下是util):

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="MyProject.Views.HomePage"
    xmlns:util="clr-namespace:Porject.Library.Util;assembly=Library"
    >

0

我正在使用Visual Studio 2015,遇到了同样的问题,并且发现网络上的答案都不是完整的解决方案。在这里,我将解释对我有效的方法。

我的主要目标是消除一直出现的红色错误消息:

当前上下文中不存在名称为InitializeComponent的内容

基本上,我创建了一个名为InitializeComponent2()的函数,它与InitializeComponent()完全相同,并使用它代替,我简单地复制了InitializeComponent()的代码。完成了任务。

它看起来像这样(我会解释):

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent2()
{
    // Change to:
    //   this.LoadFromXaml(typeof(Page2)); // for Page2
    //   this.LoadFromXaml(typeof(Page3)); // for Page3
    // and put
    //   using Xamarin.Forms.Xaml;
    // at the top of each source file.
    this.LoadFromXaml(typeof(Page1));
}

将函数放置在每个页面的定义中(.cs文件)例如:
public partial class Page1 : ContentPage
{
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
    private void InitializeComponent2()
    {
        // Change to:
        //   this.LoadFromXaml(typeof(Page2)); // for Page2
        //   this.LoadFromXaml(typeof(Page3)); // for Page3
        // and put 
        //   using Xamarin.Forms.Xaml; 
        // at the top of each source file.
        this.LoadFromXaml(typeof(Page1));
    }
}

同时,您需要在每个使用LoadFromXaml(..).cs页面顶部放置using Xamarin.Forms.Xaml;

然后当然要将InitializeComponent()更改为InitializeComponent2()以调用新函数。 也就是说,您已经将函数放入与页面相同的上下文中,使错误消失。我无法想象真正的InitializeComponent函数会在您修改项目时添加任何内容,但这是可能的。 到目前为止,对我来说一切都很好。

我尝试了许多方法,包括更改构建操作、XAML命名空间、重新启动vs、清理+重建、查找NuGet包更新等。

基本上,Visual Studio在我的Android设备+模拟器上编译和运行程序都很好,但是那个错误消息却不会消失。 我的解决方案适用于Android,但也可能适用于iOS等其他平台。

我回到了问题的根源InitializeComponent()。这个函数的实际代码是在一个叫做<Your Project Name>.Page1.xaml.g.cs或者Page2.xaml.g.cs的文件中生成的。然而,只有在某个特定事件被触发时才会生成它(文件)。我很幸运地通过在“自定义工具命名空间”中输入文本来发现它,这将触发其中一个xaml页面(以.xaml结尾的文件,而不是.cs - 确保你选择了.xaml文件),输入一些文本并按下回车键,文件就会被创建。

然后我想到了一个好主意,就是创建一个完全相同的函数InitializeComponent2(),并将其放入每个cs文件中,这样它就存在了,而不必每次都生成.xaml.g.cs文件,从而消除错误。


0

我不知道这个问题是否已经解决,但对于我来说,唯一需要做的就是删除XAML文件的第一行("xml version="1.0" encoding="utf-8")。

有时候源代码版本控制会尝试识别文件类型并添加这种内容。


0
尝试了上述所有方法均无效,对我起作用的是强制重新安装Xamarin.Forms包。在包管理器控制台提示符下执行以下命令: update-package xamarin.forms -reinstall
它不会更新包,只会删除并重新添加。

0
我在使用VS 2019社区版时遇到了同样的问题,我只是重新生成了解决方案,问题就解决了。

0
在我的情况下,问题出在项目路径上。 生成的代码文件会得到一个包含绝对路径编码后的名称,以使其成为有效的文件名。 路径是“D:\ Projekt C#\ ProjectRootFolder \ Project”。 构建工具生成的文件名是“Project.Droid.D_.Projekt_C_. Namespace等”。 将项目移动到类似“D:\ project \ ProjectRootFolder”的路径中,在我的情况下有所帮助。

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