J'ajoute des étudiants et enregistre les détails du profil dans la base de données SQLite. Les profils d'étudiants ajoutés sont affichés dans une liste comme ci-dessous.enregistrer l'image Utilisation du sélecteur de fichiers dans Windows Phone 8.1
Je veux ajouter une image de l'étudiant à l'aide d'un sélecteur de fichier et l'enregistrer. Comment puis-je y parvenir? Toute suggestion ou exemple similaire serait plus utile.
Jusqu'à présent, mes codes
private string mruToken = null;
private NavigationHelper navigationHelper;
public AddConatct()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.Loaded += LoadSchoolToCombo;
this.navigationHelper.LoadState += navigationHelper_LoadState;
this.navigationHelper.SaveState += navigationHelper_SaveState;
}
private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null && e.PageState.ContainsKey("mruToken"))
{
object value = null;
if (e.PageState.TryGetValue("mruToken", out value))
{
if (value != null)
{
mruToken = value.ToString();
// Open the file via the token that you stored when adding this file into the MRU list.
Windows.Storage.StorageFile file =
await Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruToken);
if (file != null)
{
// Open a stream for the selected file.
Windows.Storage.Streams.IRandomAccessStream fileStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Set the image source to a bitmap.
Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage =
new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bitmapImage.SetSource(fileStream);
img.Source = bitmapImage;
// Set the data context for the page.
this.DataContext = file;
}
}
}
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
if (!string.IsNullOrEmpty(mruToken))
{
e.PageState["mruToken"] = mruToken;
}
}
private async void PickPhoto_Click(object sender, RoutedEventArgs e){
Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types.
openPicker.FileTypeFilter.Clear();
openPicker.FileTypeFilter.Add(".bmp");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".jpg");
// Open the file picker.
Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();
// file is null if user cancels the file picker.
if (file != null)
{
// Open a stream for the selected file.
Windows.Storage.Streams.IRandomAccessStream fileStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Set the image source to the selected bitmap.
Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage =
new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bitmapImage.SetSource(fileStream);
img.Source = bitmapImage;
this.DataContext = file;
// Add picked file to MostRecentlyUsedList.
mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file);
}
}
private async void AddContact_Click(object sender, RoutedEventArgs e)
{
DatabaseHelperClass Db_Helper = new DatabaseHelperClass();//Creating object for DatabaseHelperClass.cs from ViewModel/DatabaseHelperClass.cs
if (NametxtBx.Text != "" & AgetxtBx.Text != "" & AddresstxtBx.Text != "" & SchoolComboBx.SelectedValue.ToString() != "" & GardienttxtBx.Text != "" & PhonetxtBx.Text != "" & LattxtBx.Text != "" & LongtxtBx.Text != "")
{
Db_Helper.Insert(new Contacts(NametxtBx.Text, AgetxtBx.Text, AddresstxtBx.Text, SchoolComboBx.SelectedValue.ToString(), GardienttxtBx.Text, PhonetxtBx.Text, LattxtBx.Text, LongtxtBx.Text));
Frame.Navigate(typeof(ReadContactList));//after add contact redirect to contact listbox page
}
else
{
MessageDialog messageDialog = new MessageDialog("Please fill all fields");//Text should not be empty
await messageDialog.ShowAsync();
}
}
Contacts.cs
public class Contacts
{
//The Id property is marked as the Primary Key
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Address { get; set; }
public string School { get; set; }
public string Gardient { get; set; }
public string PhoneNumber { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string CreationDate { get; set; }
public Contacts()
{
//empty constructor
}
public Contacts(string name, string age, string address, string school, string gardient, string phone_no, string latitude, string longitude)
{
Name = name;
Age = age;
Address = address;
School = school;
Gardient = gardient;
PhoneNumber = phone_no;
Latitude = latitude;
Longitude = longitude;
CreationDate = DateTime.Now.ToString();
}
}
Comment dans ContinueFileOpenPicker enregistrer l'image. Si c'est le meilleur moyen de sauver le chemin, comment le faire? help me –
Ici, la méthode continueFileOpenPicker enregistre l'image dans le dossier local en utilisant la méthode CopyAsync. Si vous voulez l'enregistrer dans la base de données, obtenez le fichier Path et sauvegardez-le dans la base de données. – Rohit
J'ai mangé pour enregistrer l'image en tant que base64 String et le récupérer en tant qu'image à l'aide de votre code. Maintenant, je suis confronté à des problèmes pour afficher l'image dans une vue Liste comme j'ai ouvert une autre question (http://stackoverflow.com/questions/34637185/binding-base64-string-image-into-list-view-stored-in-sqlite -database-dans-windows). J'apprécie vraiment si vous pouvez aider à ce sujet aussi. –