public async void Capturar() { //Variable para la validacion de campos numericos int num = 0; //Obtener las imagenes del XAML var imageFotoPerfil = this.ImageSource as FileImageSource; var imageCredencialFrontal = this.CredencialFrontalSource as FileImageSource; var imageCredencialPosterior = this.CredencialPosteriorSource as FileImageSource; //Obtencion de las rutas de las imagenes de XAML string fotoRuta = string.Empty; string credencialFRuta = string.Empty; string credencialPRuta = string.Empty; //Verificaciones de las cadenas de ruta de imagenes de XAML if (imageFotoPerfil == null) { fotoRuta = "0"; } else { fotoRuta = "no_image"; } if (imageCredencialFrontal == null) { credencialFRuta = "0"; } else { credencialFRuta = "no_image"; } if (imageCredencialPosterior == null) { credencialPRuta = "0"; } else { credencialPRuta = "no_image"; } //Validaciones //Validacion de Imagen de foto de perfil if (fotoRuta.Equals("no_image")) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Se necesita una foto de perfil", "Aceptar"); } //Validaciones de campos vacios else if (this.Nombre.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Nombre Vacío", "Aceptar"); } else if (this.ApellidoPat.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Apellido Paterno Vacío", "Aceptar"); } else if (this.ApellidoMat.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Apellido Materno Vacío", "Aceptar"); } else if (this.SexoSelected.Equals(this.Sexos.ElementAt(0))) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Sexo Vacío", "Aceptar"); } else if (this.Edad.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Edad Vacío", "Aceptar"); } //Validacion de campo numerico else if (!Int32.TryParse(this.Edad, out num)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo edad no es númerico", "Aceptar"); } else if (this.EstadoCivilSelected.Equals(this.EstadosCiviles.ElementAt(0))) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Estado Civil Vacío", "Aceptar"); } else if (this.Municipio.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Municipio Vacío", "Aceptar"); } else if (this.Region.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Región Vacío", "Aceptar"); } else if (!Int32.TryParse(this.Region, out num)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo región no es númerico", "Aceptar"); } /*else if (this.Zona.Equals(string.Empty)) * { * await Application.Current.MainPage.DisplayAlert( * "ERROR", * "Campo Zona Vacío", * "Aceptar"); * } * else if (!Int32.TryParse(this.Zona, out num)) * { * await Application.Current.MainPage.DisplayAlert( * "ERROR", * "Campo zona no es númerico", * "Aceptar"); * } * else if (this.Seccion.Equals(string.Empty)) * { * await Application.Current.MainPage.DisplayAlert( * "ERROR", * "Campo Sección Vacío", * "Aceptar"); * } * else if (!Int32.TryParse(this.Seccion, out num)) * { * await Application.Current.MainPage.DisplayAlert( * "ERROR", * "Campo sección no es númerico", * "Aceptar"); * }*/ else if (this.Casilla.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Casilla Vacío", "Aceptar"); } else if (this.Promotor.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Promotor Vacío", "Aceptar"); } else if (this.Comunidad.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Comunidad Vacío", "Aceptar"); } else if (this.Domicilio.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Domicilio Vacío", "Aceptar"); } else if (this.TelefonoCelular.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Teléfono Celular Vacío", "Aceptar"); } //Validacion de tamaño de la cadena else if (!this.TelefonoCelular.Length.Equals(14)) { await Application.Current.MainPage.DisplayAlert( "ERROR", string.Format("Campo Teléfono Celular tiene {0} digitos y solo deben se 10", this.TelefonoCelular.Length), "Aceptar"); } else if (this.Ocupacion.Equals(this.Ocupaciones.ElementAt(0))) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Ocupación Vacío", "Aceptar"); } else if (this.Escolaridad.Equals(this.Escolaridades.ElementAt(0))) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Escolaridad Vacío", "Aceptar"); } else if (this.Email.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Correo Electrónico Vacío", "Aceptar"); } else if (!RegexUtilities.ComprobarFormatoEmail(this.Email)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Formato Correo Electrónico Incorrecto", "Aceptar"); } else if (this.NumIne.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Número INE Vacío", "Aceptar"); } else if (!this.NumIne.Length.Equals(13)) { await Application.Current.MainPage.DisplayAlert( "ERROR", string.Format("Campo Número INE Tiene {0} dígitos y deben ser 13 dígitos", this.NumIne.Length), "Aceptar"); } else if (this.ClaveIne.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo Clave de Elector Vacío", "Aceptar"); } else if (!this.ClaveIne.Length.Equals(18)) { await Application.Current.MainPage.DisplayAlert( "ERROR", string.Format("Campo Clave de Elector Tiene {0} caracteres y deben ser 18 caracteres", this.ClaveIne.Length), "Aceptar"); } else if (this.Curp.Equals(string.Empty)) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Campo CURP Vacío", "Aceptar"); } else if (!this.Curp.Length.Equals(18)) { await Application.Current.MainPage.DisplayAlert( "ERROR", string.Format("Campo CURP Tiene {0} caracteres y deben ser 18 caracteres", this.Curp.Length), "Aceptar"); } else if (credencialFRuta.Equals("no_image")) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Foto frontal de la credencial es obligatoria", "Aceptar"); } else if (credencialPRuta.Equals("no_image")) { await Application.Current.MainPage.DisplayAlert( "ERROR", "Foto posterior de la credencial es obligatoria", "Aceptar"); } else { //Creacion de variables por default string id = null; byte[] imageArray = null; byte[] CredencialFArray = null; byte[] CredencialPArray = null; //convertir las imagenes en tipo byte[] if (this.file != null) { imageArray = FilesHelper.ReadFully(this.file.GetStream()); File.Delete("/storage/emulated/0/Android/data/com.companyname.encuestahorizonte/files/Pictures/fotoPerfil.jpg"); if (this.perfilTomado > 1) { for (int i = 1; i < this.perfilTomado; i++) { File.Delete(string.Format("/storage/emulated/0/Android/data/com.companyname.encuestahorizonte/files/Pictures/fotoPerfil_{0}.jpg", i)); } } this.file.Dispose(); } if (this.credencialFrontalfile != null) { CredencialFArray = FilesHelper.ReadFully(this.credencialFrontalfile.GetStream()); File.Delete("/storage/emulated/0/Android/data/com.companyname.encuestahorizonte/files/Pictures/testCredencialF.jpg"); if (this.frontalTomado > 1) { for (int i = 1; i < this.frontalTomado; i++) { File.Delete(string.Format("/storage/emulated/0/Android/data/com.companyname.encuestahorizonte/files/Pictures/testCredencialF_{0}.jpg", i)); } } this.credencialFrontalfile.Dispose(); } if (this.credencialPosteriorfile != null) { CredencialPArray = FilesHelper.ReadFully(this.credencialPosteriorfile.GetStream()); File.Delete("/storage/emulated/0/Android/data/com.companyname.encuestahorizonte/files/Pictures/testCredencialP.jpg"); if (this.posteriorTomado > 1) { for (int i = 1; i < this.posteriorTomado; i++) { File.Delete(string.Format("/storage/emulated/0/Android/data/com.companyname.encuestahorizonte/files/Pictures/testCredencialP_{0}.jpg", i)); } } this.credencialPosteriorfile.Dispose(); } this.perfilTomado = 0; this.frontalTomado = 0; this.posteriorTomado = 0; int rows = 0; try { //Insertar el afiliado using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation)) { conn.CreateTable <Afiliado>(); this.Afiliado = new Afiliado(); this.Afiliado = this.helperAfiliado.Llenado(id, this.Municipio, this.Region, this.Zona, this.Seccion, this.Casilla, this.Promotor, this.Comunidad, this.Nombre, this.NombreSegundo, this.ApellidoPat, this.ApellidoMat, this.SexoSelected, this.Edad, this.EstadoCivilSelected, this.Domicilio, this.TelefonoFijo, this.TelefonoCelular, this.TelefonoAlter, this.Ocupacion, this.Escolaridad, this.Email, this.NumIne, this.ClaveIne, this.Curp, this.Facebook, this.Observacion, Settings.IdUsuario, imageArray, CredencialFArray, CredencialPArray); rows += conn.Insert(this.Afiliado); } } catch (Exception e) { //Mensaje de error con la base de datos await Application.Current.MainPage.DisplayAlert( "ERROR", e.Message + "\n\nVolver a intentar", "Aceptar"); } //Caso positivo if (rows > 0) { //Resetear los campos this.Municipio = string.Empty; this.Region = string.Empty; this.Zona = string.Empty; this.Seccion = string.Empty; this.Casilla = string.Empty; this.Promotor = string.Empty; this.Comunidad = string.Empty; this.Nombre = string.Empty; this.NombreSegundo = string.Empty; this.ApellidoPat = string.Empty; this.ApellidoMat = string.Empty; this.SexoSelected = this.Sexos.ElementAt(0); this.Edad = string.Empty; this.EstadoCivilSelected = this.EstadosCiviles.ElementAt(0); this.Domicilio = string.Empty; this.TelefonoFijo = string.Empty; this.TelefonoCelular = string.Empty; this.TelefonoAlter = string.Empty; this.Ocupacion = this.Ocupaciones.ElementAt(0); this.Escolaridad = this.Escolaridades.ElementAt(0); this.Email = string.Empty; this.NumIne = string.Empty; this.ClaveIne = string.Empty; this.Curp = string.Empty; this.Facebook = string.Empty; this.Observacion = string.Empty; this.ImageSource = "no_image"; this.CredencialFrontalSource = "no_image"; this.CredencialPosteriorSource = "no_image"; this.file = null; this.credencialFrontalfile = null; this.credencialPosteriorfile = null; await Application.Current.MainPage.DisplayAlert( "EXITO", "Creación Exitosa", "Aceptar"); } else { await Application.Current.MainPage.DisplayAlert( "ERROR", "La Creación Falló", "Aceptar"); } } }