Uri uri = new Uri("/PhoneApp1;component/Image/2.png", UriKind.Relative);
//查看安装文件夹中的资源文件
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(uri);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(streamResourceInfo.Stream);
Image image = new Image();
image.Source = bitmapImage;
3)访问安装文件夹
我们通过代码获取安装目录下的所有文件和文件夹。
[C#]
//获取安装文件夹
StorageFolder installedLocation = Package.Current.InstalledLocation;
//获取安装文件夹下的子文件夹集合
var folders = await installedLocation.GetFoldersAsync();
var folderNames = folders.Select(x => x.Name).ToArray();
//获取安装文件夹下的文件集合
var files = await installedLocation.GetFilesAsync();
var fileNames = files.Select(x => x.Path).ToArray();
另外,我们还可以通过路径的方式访问安装文件夹,如下操作将访问图片文件,并展示到图片控件。
[C#]
Image image = new Image();
StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///1.jpg"));
var c = await storageFile.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(c.AsStream());
image.Source = bitmapImage;
this.stackPanel.Children.Add(image);
三、本地文件夹(独立存储空间)
1)在本地文件夹中操作文件
WP7:
[C#]
//WP7中存取独立存储空间(本地文件夹)的方法
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
//获取本地文件夹下所有的文件名集合
var fileNames = storageFile.GetFileNames();
//操作文件
var storageFileStrem = storageFile.OpenFile("test.txt", FileMode.OpenOrCreate);
}
WP8:
[C#]
//WP8中存取本地文件夹(独立存储空间)的方法
//获取本地文件夹
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
//操作文件
var file = await localFolder.GetFileAsync("test.txt");
var fileRandomAccessStream = await file.OpenAsync(FileAccessMode.Read);
var fileStream = fileRandomAccessStream.AsStream();
var path = localFolder.Path;
public class MyDataContext : DataContext
{
//定义连接字符串
public static string DBConnectionString = "Data Source=isostore:/MyDb.sdf";
public MyDataContext()
: base(DBConnectionString)
{ }
///
/// 学生表
///
public Table Students;
}
创建学生数据表:
[C#]
[Table]
public class Student : INotifyPropertyChanged, INotifyPropertyChanging
{
// 定义ID,主键字段,必备
private int _id;
///
/// 编号
///
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int Id
{
get
{
return _id;
}
set
{
if (_id != value)
{
NotifyPropertyChanging("Id");
_id = value;
NotifyPropertyChanged("Id");
}
}
}
private string _name;
///
/// 学生姓名
///
[Column]
public string Name
{
get
{
return _name;
}
set
{
if (_name != value)
{
NotifyPropertyChanging("Name");
_name = value;
NotifyPropertyChanged("Name");
}
}
}
private int _age;
///
/// 年龄
///
[Column]
public int Age
{
get
{
return _age;
}
set
{
if (_age != value)
{
NotifyPropertyChanging("Age");
_age = value;
NotifyPropertyChanged("Age");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify the page that a data context property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region INotifyPropertyChanging Members
public event PropertyChangingEventHandler PropertyChanging;
// Used to notify the data context that a data context property is about to change
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
#endregion
}
这里是对学生表进行操作的逻辑:
[XAML]
[C#]
private void Add()
{
MyDataContext db = new MyDataContext();
//创建数据库
if (!db.DatabaseExists())
db.CreateDatabase();
//添加学生
Student student1 = new Student
{
Id = 1,
Name = "张三",
Age = 15
};
db.Students.InsertOnSubmit(student1);
List students = new List();
students.Add(new Student { Id = 2, Name = "李四", Age = 16 });
students.Add(new Student { Id = 3, Name = "王五", Age = 17 });
db.Students.InsertAllOnSubmit(students);
db.SubmitChanges();
}
private void Show()
{
MyDataContext db = new MyDataContext();
//显示年龄大于15岁的学生
listbox1.ItemsSource = db.Students.Where(x => x.Age > 15);
}
private void Delete()
{
MyDataContext db = new MyDataContext();
//删除姓名为李四的学生
var query = db.Students.Where(x => x.Name == "李四");
db.Students.DeleteAllOnSubmit(query);
db.SubmitChanges();
}
private void Update()
{
MyDataContext db = new MyDataContext();
//将所有的学生年龄加一岁
foreach (var student in db.Students)
{
student.Age++;
}
db.SubmitChanges();
}