/// <summary>
        /// Método que carga un almacén. Solicita la contraseña del almacén seleccionado y carga los alias de dicho 
        /// alamacén en la lista de alias si la contraseña proporcionada fuera correcta.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void ComboBox_Stores_Selection_Changed(object sender, SelectionChangedEventArgs e)
        {
            if (pfxList.SelectedItem != null)
            {
                // Si no se ha seleccionado el elemento "dummy"...
                if (!pfxList.SelectedItem.Equals(labels.GetString("Etiqueta_seleccion_almacen")))
                {
                    IReadOnlyList<StorageFile> files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
                    StorageFile fileSelected = null;
                    foreach (StorageFile file in files)
                    {
                        if (file.Name.Equals((String)pfxList.SelectedItem))
                        {
                            fileSelected = file;
                            break;
                        }
                    }
                    if (fileSelected != null)
                    {
                        using (StreamReader reader = new StreamReader(await fileSelected.OpenStreamForReadAsync()))
                        {
                            // Pedimos el PIN del almacén al usuario
                            CredentialPanel2 cp2 = new CredentialPanel2(pfxList.SelectedItem.ToString());
                            cp2.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                            CustomDialog customDialog = new CustomDialog(cp2, labels.GetString("Etiqueta_peticion_pass"));
                            customDialog.Commands.Add(new UICommand(labels.GetString("Boton_aceptar")));
                            customDialog.Commands.Add(new UICommand(labels.GetString("Boton_cancelar")));
                            customDialog.DefaultCommandIndex = 0;
                            customDialog.CancelCommandIndex = 1;
                            IUICommand com = await customDialog.ShowAsync();
                            // El usuario ha cancelado, si solo no estaba el elemento "dummy" 
                            // hay que añadirlo en la posición cero y seleccionarlo, para permitirle reintentar
                            if (com.Label.Equals(labels.GetString("Boton_cancelar")))
                            {
                                if (!pfxList.Items.Contains(labels.GetString("Etiqueta_seleccion_almacen")))
                                {
                                    pfxList.Items.Insert(0, labels.GetString("Etiqueta_seleccion_almacen"));
                                }
                                pfxList.SelectedIndex = 0;
                            }
                            // El usuario ha aceptado en el diálogo de PIN
                            if (com.Label.Equals(labels.GetString("Boton_aceptar")))
                            {
                                aliasList.Items.Clear();
                                try
                                {
                                    store = new Pkcs12Store(reader.BaseStream, cp2.getPassword().ToCharArray());
                                    foreach (string n in store.Aliases)
                                    {
                                        if (store.IsKeyEntry(n))
                                        {
                                            AsymmetricKeyEntry key = store.GetKey(n);

                                            if (key.Key.IsPrivate)
                                            {
                                                aliasList.Items.Clear();
                                                aliasList.IsEnabled = true;
                                                RsaPrivateCrtKeyParameters parameters = key.Key as RsaPrivateCrtKeyParameters;
                                                rsaKeyParameter = (RsaKeyParameters)key.Key;
                                                foreach (object s in store.Aliases)
                                                {
                                                    aliasList.Items.Add((string)s);
                                                }

                                            }
                                        }
                                    }
                                }
                                catch
                                {
                                    AfirmaMetroUtils.showMessage(labels.GetString("Error_carga_almacen"), "Error en el almacén de claves" + " (" + pfxList.SelectedItem + ")");
                                    // Para permitirle reintentar insertamos el elemento "dummy" como primer elemento
                                    // y lo seleccionamos
                                    if (!pfxList.Items.Contains(labels.GetString("Etiqueta_seleccion_almacen")))
                                    {
                                        pfxList.Items.Insert(0, labels.GetString("Etiqueta_seleccion_almacen"));
                                    }
                                    pfxList.SelectedIndex = 0;
                                    return;
                                }

                                // Se ha seleccionado correctamente un almacén, eliminamos el componente "dummy"
                                if (pfxList.Items.Contains(labels.GetString("Etiqueta_seleccion_almacen")))
                                {
                                    pfxList.Items.Remove(labels.GetString("Etiqueta_seleccion_almacen"));
                                }

                                aliasList.SelectedIndex = 0;
                            }
                        }
                    }

                }
                else
                {
                    disableComboAlias();
                }
            }
        }
        private async void Button_Import_Click(object sender, RoutedEventArgs e)
        {

            StringBuilder contents = new StringBuilder();
            StorageFile selectedFile2 = null;
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".pfx");
            openPicker.FileTypeFilter.Add(".p12");

            if (ApplicationView.Value == ApplicationViewState.Snapped)
            {
                if (ApplicationView.TryUnsnap())
                {
                    selectedFile2 = await openPicker.PickSingleFileAsync();
                }
            }
            else
            {                
                selectedFile2 = await openPicker.PickSingleFileAsync();
            }

            if (selectedFile2 != null)
            {
                CredentialPanel cp = new CredentialPanel();
                bool foco = cp.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                CustomDialog customDialog = new CustomDialog(cp, labels.GetString("Etiqueta_peticion_pass"));
                customDialog.Commands.Add(new UICommand(labels.GetString("Boton_aceptar")));
                customDialog.Commands.Add(new UICommand(labels.GetString("Boton_cancelar")));
                customDialog.DefaultCommandIndex = 0;
                customDialog.CancelCommandIndex = 1;
                IUICommand com = await customDialog.ShowAsync();

                if (com.Label.Equals(labels.GetString("Boton_aceptar")))
                    if (cp.getPassword() != null)
                        using (StreamReader reader = new StreamReader(await selectedFile2.OpenStreamForReadAsync()))
                        {
                            char[] password = cp.getPassword().ToCharArray();
                            try
                            {
                                store = new Pkcs12Store(reader.BaseStream, password);
                                if (store != null)
                                {
                                    await selectedFile2.CopyAsync(localFolder, selectedFile2.Name, NameCollisionOption.ReplaceExisting);
                                    storeData = new StoreData();
                                    storeData.LoadData();
                                    _source = storeData.GetGroupsByCategory();
                                    ItemGridView2.ItemsSource = storeData.Collection;
                                    EvaluateDeleteButton(storeData);
                                    // Le quitamos la extensión al nombre del almacén por coherencia con el borrado, que al pillarlo de la lista no tiene extensión.
                                    AfirmaMetroUtils.showMessage(labels.GetString("Info_almacen_importado") + selectedFile2.Name.Replace(selectedFile2.FileType, "") + "\".", "Importación de almacén correcta");

                                    // Lanzamos:
                                    var options = new Windows.System.LauncherOptions();
                                    options.DisplayApplicationPicker = true;
                                    var file2 = await localFolder.GetFileAsync(selectedFile2.Name);
                                    bool success = await Windows.System.Launcher.LaunchFileAsync(file2, options);

                                }
                            }
                            catch
                            {
                                AfirmaMetroUtils.showMessage(labels.GetString("Error_carga_almacen"), "Error en la importación de almacén");
                            }
                        }
            }
        }
        /// <summary>
        /// Método que importa un almacén a la aplicación. Solicita la clave del almacén y verifica que es un almacén,
        /// en cuyo caso, almacenará dicho almacén en la carpeta de la aplicación para poder ser utilizado más tarde.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Button_Import_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder contents = new StringBuilder();

            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".pfx");
            openPicker.FileTypeFilter.Add(".p12");
            StorageFile selectedFile2 = await openPicker.PickSingleFileAsync();
            
            if (selectedFile2 != null)
            {
                CredentialPanel cp = new CredentialPanel();
                cp.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                CustomDialog customDialog = new CustomDialog(cp, labels.GetString("Etiqueta_peticion_pass"));
                customDialog.Commands.Add(new UICommand(labels.GetString("Boton_aceptar")));
                customDialog.Commands.Add(new UICommand(labels.GetString("Boton_cancelar")));
                customDialog.DefaultCommandIndex = 0;
                customDialog.CancelCommandIndex = 1;
                IUICommand com = await customDialog.ShowAsync();

                if (com.Label.Equals(labels.GetString("Boton_aceptar")))
                    if (cp.getPassword() != null)
                        using (StreamReader reader = new StreamReader(await selectedFile2.OpenStreamForReadAsync()))
                        {
                            char[] password = cp.getPassword().ToCharArray();
                            try
                            {
                                store = new Pkcs12Store(reader.BaseStream, password);
                                if (store != null)
                                {
                                    await selectedFile2.CopyAsync(localFolder, selectedFile2.Name, NameCollisionOption.ReplaceExisting);
                                    AfirmaMetroUtils.showMessage(labels.GetString("Info_almacen_importado") + selectedFile2.Name + "\"", "Importación de almacén correcta");
                                    // Recargamos los almacenes
                                    loadStorage();
                                }
                            }
                            catch
                            {
                                AfirmaMetroUtils.showMessage(labels.GetString("Error_carga_almacen"), "TITULO");
                            }
                        }
                }
             
        }