【问题标题】:Xamarin.Forms - InitializeComponent doesn't exist when creating a new pageXamarin.Forms - 创建新页面时 InitializeComponent 不存在
【发布时间】:2015-05-03 07:41:25
【问题描述】:

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

简而言之,我使用 PCL 创建了一个 Xamarin.Forms 解决方案,然后尝试将 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 项目。我似乎记得旧项目模板的一个问题。
  • 我应该提到我使用的是视觉工作室和试用版。如何检查版本或更新到最新版本?谢谢你的回答。我尝试右键单击 pcl 并从 nuget 下载最新的 xamarin-Forms。同样的问题。
  • 你能分享你的项目的源代码吗?
  • 谢谢,我更新了问题。如果您需要更多信息,请告诉我。
  • 以防万一:将解决方案移动到新文件夹后,我遇到了同样的问题。我不得不编辑 .csproj 文件来手动更新“packages”文件夹,然后它碰巧再次编译。

标签: xamarin xamarin.forms


【解决方案1】:

尝试了以上所有方法,但没有运气,对我有用的是强制重新安装 Xamarin.Forms 包。在包管理器控制台提示符下;

更新包 xamarin.forms -重新安装

它不更新包,只是删除和重新添加

【讨论】:

    【解决方案2】:

    我在根元素中缺少一个特定的引用属性(在我的例子中是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"
        >
    

    【讨论】:

      【解决方案3】:

      我在 VS 社区版 2019 中遇到了同样的问题,我只是使用了 regenerate 解决方案,问题已得到解决

      【讨论】:

        【解决方案4】:

        对我来说,这只是一个流氓 XAML,在&lt;?xml version="1.0" encoding="utf-8" ?&gt; 之前有一个前导空格,即

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

        导致一堆CS0103 The name 'InitializeComponent' does not exist in the current context 错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-17
          • 2016-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多