当前上下文中不存在名称 'InitializeComponent'

The name 'InitializeComponent' doesn't exist in the current context

我知道这个错误有很多答案,但都是一些系统错误之类的。我刚刚创建了这个应用程序并且运行良好。然后我添加了一个视图模型来处理我在注册和登录页面之间的导航,并在 login/register.xaml 中更新了文本以了解我在哪个页面上。但是现在InitializeComponent无法识别

我只放了注册页面,因为登录是相同的,但登录名:

using Xamarin.Forms;

namespace Appointments.Views
{
    public partial class RegisterPage : ContentPage
    {
        public RegisterPage()
        {
            InitializeComponent();
        }
    }
}

和视图模型:

using Appointments.Views;
using MvvmHelpers;
using MvvmHelpers.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Appointments.ViewModels
{
    public class RegLogViewModel : BaseViewModel
    {   
        public AsyncCommand GoToRegisterCommand;
        public AsyncCommand GoToLoginCommand;
        RegLogViewModel()
        {
            GoToLoginCommand = new AsyncCommand(GoToLogin);
            GoToRegisterCommand = new AsyncCommand(GoToRegister);
        }

        async Task GoToRegister()
        {
            await Shell.Current.GoToAsync($"//{ nameof(RegisterPage)}");
        }

        async Task GoToLogin()
        {
            await Shell.Current.GoToAsync($"//{ nameof(LoginPage)}");
        }   
    }
}

和Register.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="Appoitments.Views.RegisterPage">
    
    <ContentPage.Content>
        <StackLayout>
            <Label 
                Text="Register Page!"
                BackgroundColor="Blue"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />

            <Button
                Text="Go To Login"
                Command="{Binding GoToLoginCommand}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

您的 Register.xaml 中存在拼写错误,导致 xaml 和 Register class 的 cs 部分定义之间存在不同的命名空间。

你有

x:Class="Appoitments.Views.RegisterPage"

而不是

x:Class="Appointments.Views.RegisterPage"