Xamarin.Forms-创建新页面时不存在InitializeComponent [英] Xamarin.Forms - InitializeComponent doesn't exist when creating a new page

查看:218
本文介绍了Xamarin.Forms-创建新页面时不存在InitializeComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Visual Studio to try out Xamarin.Forms. I'm trying to follow the guide: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getting_started_with_xaml/

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

In short, I create a Xamarin.Forms solution using a PCL and then try to add a Forms XAML Page to the PCL-project.

创建的隐藏代码如下:

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

这里的问题是InitializeComponent();是红色的. 当我尝试构建时,我得知The name 'InitializeComponent' does not exist in the current context

The problem here is that InitializeComponent(); is red. When I try to build I get informed that The name 'InitializeComponent' does not exist in the current context

我一直在寻找解决方案,即使其他人也遇到了同样的麻烦,他们的解决方案也对我不起作用.这是我尝试使用的一项建议: http://blog.falafel. com/xamarin-error-initializecomponent-在当前上下文中不存在/

I've been looking around for solutions and even though others have had the same trouble, their solutions wont work for me. Here is one suggestion i tried to use: http://blog.falafel.com/xamarin-error-initializecomponent-does-not-exist-in-the-current-context/

如果您有解决此问题的方法,请告诉我.谢谢!

Please let me know if you have a solution for this problem. Thanks!

更新:

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

My PCL (which is where I also want to add my XAML-page) contains:

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页面:

And my XAML-page:

<?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();
        }
    }

推荐答案

更新: 这个错误通常不会在VS 2015中出现,如果确实出现,这是我的原始答案:

UPDATE: This error doesn't usually appear in VS 2015, if it does, here's my original answer:

找到了解决方案! 右键单击.XAML文件,选择属性.

Found the solution! Right click on the .XAML file, select Properties.

您将看到一个名为自定义工具的属性.将其值从MSBuild:Compile更改为MSBuild:UpdateDesignTimeXaml

You will see a Property called Custom Tool. Change its value from MSBuild:Compile to MSBuild:UpdateDesignTimeXaml

这将解决问题. 不了解下降投票,但这是我的屏幕截图:

This will solve the problem. Dont know about the downvote, but here's my screenshot:

更新:

它很少再出现.如果是这样,只需打开Xaml并在文件后面编码并保存即可.我知道,这不是最好的解决方案,但可以完成工作.

It reappears rarely. If it does, just open the Xaml and code behind files and save them. I know, its not the best solution, but it gets the job done.

这篇关于Xamarin.Forms-创建新页面时不存在InitializeComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆