简体   繁体   English

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

[英]The name 'InitializeComponent' doesn't exist in the current context

I know that this error has many answers, but they are all some system errors or smth like that.我知道这个错误有很多答案,但它们都是一些系统错误或类似的东西。 I just created this app and it worked fine.我刚刚创建了这个应用程序,它运行良好。 Then I added a view-model to handle my navigation between the register and login page and in login/register.xaml I updated the text to know on what page I'm.然后我添加了一个视图模型来处理我在注册和登录页面之间的导航以及在 login/register.xaml 我更新了文本以了解我在哪个页面上。 But now InitializeComponent is not recognized.但现在InitializeComponent无法识别。

I put only Register page because login is the same but with login name:我只放了注册页面,因为登录名是相同的,但有登录名:

using Xamarin.Forms;

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

And the view-model:和视图模型:

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)}");
        }   
    }
}

And Register.xaml:并注册.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>

There is a typo mistake in your Register.xaml , that results in different namespace between the xaml and the cs partial definition of your Register class. Register.xaml中存在拼写错误,导致 xaml 和Register class 的 cs 部分定义之间存在不同的命名空间。

You have你有

x:Class="Appoitments.Views.RegisterPage"

instead of代替

x:Class="Appointments.Views.RegisterPage"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称InitializeComponent在当前上下文中不存在 - The name InitializeComponent does not exist in the current context 新的WPF项目无法编译-引发错误“名称&#39;InitializeComponent&#39;在当前上下文中不存在” - New WPF Project Won't Compile — Throws Error “The name 'InitializeComponent' does not exist in the current context” 当前上下文中不存在“ InitializeComponent” - 'InitializeComponent' does not exist in the current context CS0103,与其他 XAML 页面一起使用时,当前上下文中不存在“InitializeComponent” - CS0103, 'InitializeComponent' doesn't exist in current context when working with other XAML pages 当前上下文中不存在名称“InitializeComponent”(c#,xamarin) - The name 'InitializeComponent' does not exist in the current context(c#,xamarin) 我的 WPF 应用程序的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in my WPF application WPF应用程序中的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in WPF application 当前上下文中不存在 Print() 名称 - Print() name doesn't exist in current context .NET Maui 当前上下文中不存在名称“InitializeComponent” - .NET Maui The name 'InitializeComponent' does not exist in the current context 当前上下文中不存在“名称” - The 'name' doesn't exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM