我正在添加学生并将配置文件详细信息保存到SQLite数据库。添加的学生资料显示在列表视图中,如下所示。
我想使用文件选择器添加学生的照片并保存。我该如何实现?任何建议或类似示例都将更有帮助。
到目前为止,我的代码
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();
}
}
参考方案
这不是一个好主意。您应该将文件保存在本地文件夹中,并保存到数据库的路径。用这种方法
您需要根据用户名或其他任何唯一属性将图像保存在本地文件夹中。我是这样做的。首先选择文件
private void choose_galary_pic_tapped(object sender, TappedRoutedEventArgs e)
{
try
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
openPicker.PickSingleFileAndContinue();
}
catch { }
}
现在用于保存文件
public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
try
{
if (args.Files.Count > 0)
{
StorageFile sf = args.Files[0];
await sf.CopyAsync(ApplicationData.Current.LocalFolder, args.Files[0].Name, NameCollisionOption.ReplaceExisting);
System.Diagnostics.Debug.WriteLine(sf.Name);
ApplicationData.Current.LocalSettings.Values["GImage"] = sf.Name;
var stream = await args.Files[0].OpenAsync(Windows.Storage.FileAccessMode.Read);
await bitmapImage.SetSourceAsync(stream);
userImage.Source = bitmapImage;
}
else
{
}
}
catch { }
}
或者,如果您想直接保存图像,则Base-64是将图像存储在SQLite中的最佳编码技术。尝试以下给定的代码。一种方法将为您提供以64为基数编码的StorageFile字符串,另一种方法将返回BitmapImage对象,可以将其设置为<Image />
的源。
private async Task<BitmapImage> Base64StringToBitmap(string source)
{
var ims = new InMemoryRandomAccessStream();
var bytes = Convert.FromBase64String(source);
var dataWriter = new DataWriter(ims);
dataWriter.WriteBytes(bytes);
await dataWriter.StoreAsync();
ims.Seek(0);
var img = new BitmapImage();
img.SetSource(ims);
return img;
}
private async Task<string> ConvertStorageFileToBase64String(StorageFile imageFile)
{
var stream = await imageFile.OpenReadAsync();
using (var dataReader = new DataReader(stream))
{
var bytes = new byte[stream.Size];
await dataReader.LoadAsync((uint)stream.Size);
dataReader.ReadBytes(bytes);
return Convert.ToBase64String(bytes);
}
}
希望能帮助到你。
Windows Phone WNS通知导航到特定页面 - c#它是Windows运行时,Windows Phone专用项目。我正在使用Azure和Azure通知中心。所以我的问题是,有谁愿意如何导航到某些特定页面并发送ID等参数。这是我的吐司模板,如字符串中所述: var toast = @"<toast><visual><binding template=""…
Windows Phone 7-C#如何在代码中使用ListPickerItem而不会崩溃? - c#到目前为止,Listpickeritem似乎有点头痛,因为它很容易崩溃。我有以下代码,在运行应用程序时选择listpickeritem时,该代码会崩溃:for (int i = 1; i <= 100; i++) { ListPickerItem item = new ListPickerItem(); item.Content = i.ToStrin…
从Windows Phone 7设备静默发送电子邮件? - c#是否可以从Windows Phone 7设备静默发送电子邮件?我问的原因是因为我想拥有一个系统,应用程序将从该系统将信息发送到该服务器将要记录的服务器。我认为如果我使用电子邮件,它将比其他系统更简单。正如您可能从我的问题中猜到的那样,我现在处在完全未开发的领域。 参考方案 只要您在服务器上运行SMTP服务器,就可以发送电子邮件。Web服务是为这种事情而设计的…
如何从php中获取datatables jQuery插件的json数据 - php我是PHP的新手,正在尝试使用Datatables jQuery插件。我知道我必须从.php文件中获取数据,然后在数据表中使用它,但是我似乎无法通过ajax将其传递给数据表。我设法从数据库中获取数据,并对其进行json编码,但是后来我不知道如何在我的index.php文件中调用它,并在其中显示它。我究竟做错了什么?这是我的代码:HTML(已编辑): <…
如何使用Cordova在Windows Phone 8上使用现有的sqlite数据库 - c#我正在使用Cordova 3.0构建一个混合应用程序。我使用插件将数据存储在sqlite数据库中。在android和ios上,我有一些脚本,这些脚本将数据库复制到应用程序的“ documents”文件夹中,在这里我可以读取/写入该预填充数据库。现在我想在Windows Phone 8上将其存档。我找到了以下教程:http://wp.qmatteoq.com/…