using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Main_Page.Pages;
using Main_Page;
using Windows.Storage.AccessCache;
using Windows.Graphics.Imaging;
using DataAccessLibrary;
using System.Collections.ObjectModel;
using System.Threading;
namespace Main_Page.Models
{
public class Items
{
public SoftwareBitmapSource AccessSource { set; get; }
public string Date { set; get; }
public string Size { set; get; }
public string Name { set; get; }
public string Path { set; get; }
}
public class PicturePicker
{
public static async Task<SoftwareBitmapSource> GetitemsAsync(int n)
{
var source = new SoftwareBitmapSource();
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileOpenPicker.FileTypeFilter.Add(".jpg");
fileOpenPicker.FileTypeFilter.Add(".png");
fileOpenPicker.FileTypeFilter.Add(".bmp");
fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
var inputFile = await fileOpenPicker.PickSingleFileAsync();
if (inputFile == null)
{
// The user cancelled the picking operation
return source;
}
SoftwareBitmap softwareBitmap;
using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
{
// Create the decoder from the stream
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
// Get the SoftwareBitmap representation of the file
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
}
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
{
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
await source.SetBitmapAsync(softwareBitmap);
// Set the source of the Image control
return source;
}
}
//添加程序未来可访问列表许可
public class GetUserPermissions {
public static string mruToken { set; get; }
public static string faToken { set; get; }
public static async Task<string> GetAccessPermissions() {
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
System.DateTime currentTime = new System.DateTime();
currentTime = System.DateTime.Now;
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
// Add to MRU with metadata (For example, a string that represents the date)
mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(folder, currentTime.ToShortDateString());
// Add to FA without metadata
faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder, currentTime.ToShortDateString());
}
else
{
return "Operation cancelled.";
}
return folder.Path;
}
}
public class ItemMannager {
//从未来可访问列表中的文件夹传回ObservableCollection<Items>列表
public static async Task ItemsAccessSourceAsync(ObservableCollection<Items> AccessSource) {
var Item = await AdditemsAsync();
AccessSource.Clear();
var Items1 = Item;
Items1.ForEach(p => AccessSource.Add(p));
}
//读取数据库并返回List<Items>
public static async Task<List<Items>> AdditemsAsync()
{
var Tokens = new List<String>();
var ItemList = new List<Items>();
Tokens = faTokenDataAccess.GetData();//返回数据库里面所有文件访问令牌faToken : List<string>
foreach (String Token in Tokens) {
var Bitmaplist =await ItemAccess.GetitemsAsync(Token);//直接运行程序跳出主页无显示(单步执行没问题)
foreach (SoftwareBitmapSource Bitmap in Bitmaplist)
ItemList.Add(new Items { AccessSource = Bitmap, Date = "2019/4/16", Size = "30", Name = "NULL", Path = "C:/Users/I1661/AppData/Local" });
}
return ItemList;
}
}
//得到文件夹中所有图片并返回List<SoftwareBitmapSource>
public static class ItemAccess
{
public static async Task<List<SoftwareBitmapSource>> GetitemsAsync(string Token)
{
var ListofBitmap = new List<SoftwareBitmapSource>();
var source = new SoftwareBitmapSource();
var inputFloder= await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(Token);
var inputFiles =inputFloder.GetFilesAsync();
var inputFiles_ = inputFiles.GetResults();
foreach (StorageFile inputFile in inputFiles_)
{
SoftwareBitmap softwareBitmap;
using (IRandomAccessStream stream = inputFile. OpenAsync(FileAccessMode.Read).GetResults())
{
// Create the decoder from the stream
var decodert = BitmapDecoder.CreateAsync(stream);
BitmapDecoder decoder = decodert.GetResults();
// Get the SoftwareBitmap representation of the file
var softwareBitmapt = decoder.GetSoftwareBitmapAsync();
softwareBitmap= softwareBitmapt.GetResults();
}
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
{
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
await source.SetBitmapAsync(softwareBitmap);
// Set the source of the Image control
ListofBitmap.Add(source);
}
return ListofBitmap;
}
}
}
线程 0x3f38 已退出,返回值为 0 (0x0)。
线程 0x5618 已退出,返回值为 0 (0x0)。
引发的异常:“System.InvalidOperationException”(位于 Main_Page.exe 中)
WinRT 信息: 在意外的时间调用了方法。
引发的异常:“System.InvalidOperationException”(位于 System.Private.CoreLib.dll 中)
WinRT 信息: 在意外的时间调用了方法。
引发的异常:“System.InvalidOperationException”(位于 System.Private.CoreLib.dll 中)
WinRT 信息: 在意外的时间调用了方法。
直接启动看到Bitmaplist里面的值是
Id = 6, Status = Faulted, Method = "{null}", Result = "{Not yet computed}"
单步就没有问题