I'm using MVVM architecture to build my app. On the registration screen, I am able to bind all Text Entries if I don't use the IsVisible and BindingContext properties that are tied to the switch.
I tested all fields they work ok. But if I add:
IsVisible="{Binding IsToggled}" BindingContext="{x:Reference IsProviderSwitch}"
in the stacklayout opening tag all the entries that are below the switch don't send the entries to create the Proveedor object, thus, getting an error.
This is the code:
<?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>
ViewModel file
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");
}
}
}
The images show what I want to accomplish. Just to be able to hide/unhide and bind the bottom section of the screen as well. Thanks for your help! ML
Try by specifying binding Source
:
<StackLayout IsVisible="{Binding Source={x:Reference IsProviderSwitch}, Path=IsToggled}">