微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决当前上下文中不存在名称“InitializeComponent”

我知道这个错误有很多答案,但它们都是一些系统错误或类似的错误。我刚刚创建了这个应用程序,它运行良好。然后我添加一个视图模型来处理我在注册登录页面间的导航,在 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)}");
        }   
    }
}

注册.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 中存在拼写错误,导致 Register 类的 xaml 和 cs 部分定义之间存在不同的命名空间。

你有

x:Class="Appoitments.Views.RegisterPage"

代替

x:Class="Appointments.Views.RegisterPage"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。