为什么我的视图不能识别来自视图模型的电子邮件和密码字符串。它说电子邮件和密码在当前上下文中不存在。有人能告诉我为什么这段代码不起作用并解释解决方案吗?这是视图中的代码:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Papucometar.ViewModel;
using Xamarin.Essentials;
namespace Papucometar.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginUI : ContentPage
{
public LoginUI()
{
var vm = new LoginViewModel();
this.BindingContext = vm;
vm.DisplayInvalidLoginPrompt += () => DisplayAlert("Error", "Invalid Login, try again", "OK");
InitializeComponent();
email.Completed += (object sender, EventArgs e) =>
{
Password.Focus();
};
Password.Completed += (object sender, EventArgs e) =>
{
vm.SubmitCommand.Execute(null);
};
}
}
}
这是ViewModel代码:
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Papucometar.ViewModel
{
public class LoginViewModel : INotifyPropertyChanged
{
public Action DisplayInvalidLoginPrompt;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private string Email;
public string email
{
get { return Email; }
set
{
Email = value;
PropertyChanged(this, new PropertyChangedEventArgs("Email"));
}
}
private string password;
public string Password
{
get { return password; }
set
{
password = value;
PropertyChanged(this, new PropertyChangedEventArgs("Password"));
}
}
public ICommand SubmitCommand { protected set; get; }
public LoginViewModel()
{
SubmitCommand = new Command(OnSubmit);
}
public void OnSubmit()
{
if (email != "macoratti@yahoo.com" || password != "secret")
{
DisplayInvalidLoginPrompt();
}
}
}
}
IntelliSense说这些变量不存在。我不明白为什么它们不能互相看到。这是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="Papucometar.View.LoginUI"
xmlns:local="clr-namespace:Papucometar.ViewModel"
x:DataType="local:LoginViewModel"
BackgroundColor="#112B47">
<ContentPage.Content>
<StackLayout Orientation="Vertical" Padding="30" Spacing="40">
<BoxView HeightRequest="10"/>
<Image HorizontalOptions="Center" WidthRequest="300" Source="maco.jpg"/>
<Frame BackgroundColor="#BF043055" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Entry x:Name="Email" Text="{Binding Email}" Placeholder="Email"
PlaceholderColor="White" HeightRequest="40"
Keyboard="Email"
TextColor="White"/>
<Entry x:Name="Password" Text="{Binding Password}" Placeholder="Senha"
PlaceholderColor="White" HeightRequest="40"
IsPassword="True"
TextColor="White"/>
</StackLayout>
</Frame>
<Button Command="{Binding SubmitCommand}" Text="Login" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
1条答案
按热度按时间14ifxucb1#
你已经完成了大部分的工作。但是,正如ToolmakerSteve所提到的,大写约定和一些细节还需要注意。
在ViewModel中,您可以按以下方式定义Email属性:
也就是说Email字符串是私有变量,只有在同一个类中才能访问。
如果一个外部类试图访问它,它可以通过你代码中的email属性来完成。所以其他人只能通过email属性访问Email字符串,明确地说,通过email属性中的get方法。
另一个是大写约定,通常我们将私有字段设置为小写,而属性名设置为大写。
所以你可以把电子邮件改成这样:
希望能成功。