我正在使用 MVVM 架构来构建我的应用程序。 在注册屏幕上,如果我不使用绑定到开关的 IsVisible 和 BindingContext 属性,我可以绑定所有文本条目。
我测试了所有可以正常工作的字段。但如果我添加:
IsVisible="{Binding IsToggled}" BindingContext="{x:Reference IsProviderSwitch}"
在 stacklayout 开始标记中,开关下方的所有条目都不会发送这些条目来创建 Proveedor 对象,因此会出现错误。
这是代码:
<?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="miturnoya.Views.RegistrationPage">
<StackLayout VerticalOptions="Center"
Margin="20">
<Label Text="Formulario de Registro"
FontSize="Large"
TextColor="Black"
HorizontalOptions="Center"
Margin="5"/>
<Entry x:Name="contactNameEntry"
Text="{Binding ContactName}"
Placeholder="Nombre"
Keyboard="Text"/>
<Entry x:Name="celularNumberEntry"
Text="{Binding CelularNumber}"
Placeholder="Número Celular"
Keyboard="Telephone"/>
<Entry x:Name="celularCompanyEntry"
Text="{Binding CelularCompany}"
Placeholder="Empresa Celular"
Keyboard="Text"/>
<Entry x:Name="emailEntry"
Text="{Binding Email}"
Placeholder="Email"
Keyboard="Email"/>
<Entry x:Name="passwordEntry"
Text="{Binding Password}"
Placeholder="Contraseña"
IsPassword="True"/>
<Entry x:Name="confirmPasswordEntry"
Text="{Binding ConfirmPassword}"
Placeholder="Confirmar Contraseña"
IsPassword="True"/>
<StackLayout Orientation="Horizontal" Spacing="10">
<Label Text="Es Proveedor?"/>
<Label Text="No/Si"/>
<Switch x:Name="IsProviderSwitch"
HorizontalOptions="CenterAndExpand"
OnColor="DeepSkyBlue"
IsToggled="{Binding IsProvider}"
ThumbColor="{StaticResource MainButtonColor}"/>
</StackLayout>
<StackLayout IsVisible="{Binding IsToggled}" BindingContext="{x:Reference IsProviderSwitch}">
<Entry
x:Name="idProveedor"
Text="{Binding IdProveedor}"
Placeholder="Id Proveedor"
Keyboard="Text"/>
<Entry
x:Name="providerCompanyEntry"
Text="{Binding RazonSocial}"
Placeholder="Razon Social"
Keyboard="Text"/>
<Entry
x:Name="cuitEntry"
Text="{Binding Cuit}"
Placeholder="CUIT"
Keyboard="Numeric"/>
<Entry
x:Name="domicilioFiscalEntry"
Text="{Binding DomicilioFiscal}"
Placeholder="Domicilio Fiscal"
Keyboard="Text"/>
</StackLayout>
<Button
x:Name="RegistrationButton"
Command="{Binding RegisterCommand}"
Text="Registrarse"
BackgroundColor="{StaticResource MainButtonColor}"
TextColor="White"
Margin="60,20,60,20"/>
</StackLayout>
</ContentPage>
视图模型文件
public class RegistrationPageViewModel : INotifyPropertyChanged
{
IUserService _userService;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(name));
}
public RegistrationPageViewModel(IUserService userService)
{
_userService = userService;
}
private List<Usuario> usuariosList;
public List<Usuario> UsuariosList
{
get { return usuariosList; }
set
{
usuariosList = value;
OnPropertyChanged("UsuariosList");
}
}
private string contactName;
public string ContactName
{
get { return contactName; }
set
{
contactName = value;
OnPropertyChanged("ContactName");
}
}
private string celularNumber;
public string CelularNumber
{
get { return celularNumber; }
set
{
celularNumber = value;
OnPropertyChanged("CelularNumber");
}
}
private string celularCompany;
public string CelularCompany
{
get { return celularCompany; }
set
{
celularCompany = value;
OnPropertyChanged("CelularCompany");
}
}
private string email;
public string Email
{
get { return email; }
set
{
email = value;
OnPropertyChanged("Email");
}
}
private string password;
public string Password
{
get { return password; }
set
{
password = value;
OnPropertyChanged("Password");
}
}
private string confirmPassword;
public string ConfirmPassword
{
get { return confirmPassword; }
set
{
confirmPassword = value;
OnPropertyChanged("ConfirmPassword");
}
}
private bool isProvider;
public bool IsProvider
{
get { return isProvider; }
set
{
isProvider = value;
OnPropertyChanged("IsProvider");
}
}
private string userType;
public string UserType
{
get { return userType; }
set
{
userType = value;
OnPropertyChanged("UserType");
}
}
private long idProveedor;
public long IdProveedor
{
get { return idProveedor; }
set
{
idProveedor = value;
OnPropertyChanged("IdProveedor");
}
}
private string razonSocial;
public string RazonSocial
{
get { return razonSocial; }
set
{
razonSocial = value;
OnPropertyChanged("RazonSocial");
}
}
private string cuit;
public string Cuit
{
get { return cuit; }
set
{
cuit = value;
OnPropertyChanged("Cuit");
}
}
private string domicilioFiscal;
public string DomicilioFiscal
{
get { return domicilioFiscal; }
set
{
domicilioFiscal = value;
OnPropertyChanged("DomicilioFiscal");
}
}
public Command RegisterCommand
{
get
{
return new Command(() =>
{
if (Password == ConfirmPassword)
RegisterAll();
else
App.Current.MainPage.DisplayAlert("", "Password must be same as above!", "OK");
});
}
}
private async void RegisterAll()
{
if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
await App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
else
{
if (isProvider)
userType = "Proveedor";
else
userType = "Cliente";
//Instantiate Usuario
Usuario user = new Usuario(email, password, isProvider, ContactName, CelularNumber, CelularCompany, userType);
if (user != null)
{
// Register Usuario y Proveedor
if(user.TipoUsuario == "Proveedor")
{
//Create[Post] user in cloud.
await _userService.CreateUser(user);
//Instantiate Proveedor
Proveedor proveedor = new Proveedor(idProveedor, razonSocial, domicilioFiscal, cuit);
//Create[Post] proveedor in cloud.
await _userService.CreateProvider(proveedor);
await App.Current.MainPage.DisplayAlert("Proveedor Registrado Exitosamente!", "", "Ok");
await App.Current.MainPage.Navigation.PushAsync(new AppointmentsAdminStorePage());
}
else // Register Usuario Cliente
{
await _userService.CreateUser(user);
await App.Current.MainPage.DisplayAlert("Cliente Registrado Exitosamente!", "", "Ok");
//Navigate to MapPage after success
await App.Current.MainPage.Navigation.PushAsync(new MapPage());
}
}
else
{
await App.Current.MainPage.DisplayAlert("Error", "SignUp Fail", "OK");
}
}
}
图像显示了我想要完成的任务。 只是为了能够隐藏/取消隐藏和绑定屏幕的底部。 谢谢你的帮助! 毫升
答案 0 :(得分:0)
尝试通过指定绑定Source
:
<StackLayout IsVisible="{Binding Source={x:Reference IsProviderSwitch}, Path=IsToggled}">