qq_22659623 2019-04-21 00:43 采纳率: 100%
浏览 387
已采纳

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条回答 默认 最新

  • weixin_41352006 2019-04-21 10:05
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我调试idea基于spring boot项目
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 分析下图所示同步时序逻辑电路的逻辑功能。
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合