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

0
在Xaml页面属性中,只需设置
Build Action = 嵌入式资源
它适用于Visual Studio 2017。

0

对我来说,这只是一个带有前导空格的流氓XAML,在<?xml version="1.0" encoding="utf-8" ?>之前,即。

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
...

导致一堆CS0103“InitializeComponent”名称在当前上下文中不存在错误。

0

添加 using Xamarin.Forms.Xaml

在 Visual Studio 2017 上测试通过


0
  1. 选择存在问题的类中的 App.xamlMainPage.xaml 文件。
  2. 右键单击并选择属性。
  3. 在“生成操作”中选择“嵌入式资源”。
  4. 在“自定义工具”中输入“MSBuild:UpdateDesignTimeXaml”。
  5. 清理并构建,问题就解决了。

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