哈马林。当前上下文中不存在名称“authorEntry”

kho*_*hoi 3 c# forms xaml xamarin

我是 Xamarin 的新手。我想向Book班级添加信息,然后使用displayalert()方法显示信息并且它有效。但是为什么会出错?希望你们能帮我解决这个问题。先谢谢你。

新书页.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="Form.NewBookPage">
        <StackLayout Margin="10,10,10,0">

            <Entry x:Name="nameEntry"
                Placeholder="name of the book"/>

            <Entry x:Name="authorEntry"
                   Placeholder="name of the author"/>

        <Button Text="save"
               Clicked="Button_Clicked" />

    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

新书页.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Form
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewBookPage : ContentPage
    {
        public NewBookPage ()
        {
            InitializeComponent ();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Book book = new Book()
            {
                Name =  nameEntry.Text,
                Author = authorEntry.Text

            };
            DisplayAlert("Success",book.Name+"/"+book.Author, "great!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误列表 在此处输入图片说明

图片

在此处输入图片说明

kho*_*hoi 5

1.清理你的项目

2.重新构建它,将您的 XAML 文件属性(构建操作)从嵌入式资源更改为编译,重新构建您的项目(会抛出错误)。

3.将您的 XAML 文件属性切换回嵌入式资源,重建。

请参阅此链接:https : //forums.xamarin.com/discussion/96447/the-name-initializecomponent-does-not-exist-in-the-current-context