.NET Maui 绑定警告,在 LoginPageViewModes 上找不到“LoginCommand”属性

问题描述 投票:0回答:1

我正在按照 AshProgHelp 的教程为我的 Windows 应用程序构建一个简单的登录页面。

在第 2 分钟 9:15 中,值被放置在用户名和密码的输入字段中,然后登录。

当我尝试同样的操作时,登录按钮没有任何反应。

检查输出日志时,我发现以下编译警告:

Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics:警告:在“MyAdministrationApp.Viewmodes.LoginPageViewModes”上找不到“LoginCommand”属性,目标属性:“Microsoft.Maui.Controls.Button.Command”

下面是.cs代码

`using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MyAdministrationApp.Models;
using MyAdministrationApp.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using MyAdministrationApp.Views;

namespace MyAdministrationApp.Viewmodes
{
    public partial class LoginPageViewModes : BaseViewModel
    {
        [ObservableProperty]
        private string _userName;

        [ObservableProperty]
        private string _password;

        readonly LoginRepository loginRepository= new LoginService();

        [RelayCommand]
        public async Task LoginCommand()
        {
            Console.WriteLine("Sign In button pressed");
            if (!string.IsNullOrWhiteSpace(UserName ) && !string.IsNullOrWhiteSpace(Password))
         
   {
                UserInfo userInfo = await loginRepository.Login(UserName, Password);

                if (Preferences.ContainsKey(nameof(App.UserInfo)))
                {
                    Preferences.Remove(nameof(App.UserInfo));
                }

                string userDetails = JsonConvert.SerializeObject(userInfo);
                Preferences.Set(nameof(App.UserInfo), userDetails);
                App.UserInfo = userInfo;

                await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
            }
        }

    }
}`

The LoginPage Class.

`using MyAdministrationApp.Viewmodes;

namespace MyAdministrationApp;

public partial class LoginPage : ContentPage
{
    public LoginPage(LoginPageViewModes loginPageViewModes)
    {
        InitializeComponent();
        this.BindingContext = loginPageViewModes;

    }
}`
and the XAML code
          <Button Text="Sign In" BackgroundColor="#58D68D" TextColor="White" FontAttributes="Bold" CornerRadius="30" WidthRequest="200" Margin="0,15,0,0" Command="{Binding LoginCommand}"/>
            <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" Margin="0,60,0,0" Padding="0">
                <Label Text="New User?" TextColor="DarkGray" FontSize="Small" Margin="0,0,20,0"/>
                <Label Text="Sign Up" TextColor="#1D8348" FontSize="Small"/>
            </StackLayout>
        </StackLayout>
    </Grid>




I've tried adding the property manually by adding this to the loginpageviewmodes

` public ICommand LoginCommand { get; set; }
public LoginPageViewModes()
    {
        LoginCommand = new Command(OnLogin);
    }

    private void OnLogin(){
    //Same code as in LoginCommand
}`

This provided me with more errors, 

The code should simply check if both input fields are not 'black' and then, upon pressing the sign in button. Should navigate to the next page. 

I am a beginner with .NET Maui.
Any help would be greatly appreciated!.

c# .net maui
1个回答
0
投票

Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics:警告:在“MyAdministrationApp.Viewmodes.LoginPageViewModes”上找不到“LoginCommand”属性,目标属性:“Microsoft.Maui.Controls.Button.Command”

此警告表明绑定命令失败。这是因为您同时使用了

[RelayCommand]
属性并将该方法命名为
LoginCommand()

请将

public async Task LoginCommand()
更改为
public async Task Login()
[RelayCommand]
将自动使登录方法成为登录命令。如:

[RelayCommand]
public async Task Login()
{

更多信息可以阅读官方文档

[RelayCommand]
属性如何工作

© www.soinside.com 2019 - 2024. All rights reserved.