UWP直接运行程序跳出主页无显示(单步执行没问题)C#

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}"
单步就没有问题

1个回答

问题就出在你采用了异步编程 ,你单步调试没问题,是因为调试时间长,已经完成了操作,直接运行就会你上面所说的那种情况,你这里必须要使用异步吗,

qq_22659623
qq_22659623 是的必须用因为那个方法没有非异步的,我当时是因为在一个同步方法中调用了异步方法出错的,当时对async和await理解不够充分.........现在解决了
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐