【问题标题】:GetFilesAsync stops workingGetFilesAsync 停止工作
【发布时间】:2012-12-18 09:42:35
【问题描述】:

我有这段代码

public static class Storage
{
    public async static Task<bool> Exists(string filename)
    {
        var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
        var _files= await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);

        var file = _files.FirstOrDefault(x => x.Name == filename);
        return file != null;
    }
}

并从我的 Windows 8 应用商店应用程序中调用它;

this.IconExists = this.Game != null && Storage.Exists(this.IconName).Result;

因此,如果我在上面的行上设置一个断点并逐步运行它,它可以工作,但没有中断并且仅运行应用程序会导致应用程序挂起。

几天前的提交中也有类似的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Storage.Search;

namespace eggrr.Core.FileStorage
{
    public class Storage
    {
        private IReadOnlyList<StorageFile> _files;

        public Storage()
        {
            _files = GetFilesAsync("Assets").Result;
        }

        private async Task<IReadOnlyList<StorageFile>> GetFilesAsync(string relativeFolderPath)
        {
            var path = string.Format("{0}\\{1}", Package.Current.InstalledLocation.Path, relativeFolderPath);
            var folder = await StorageFolder.GetFolderFromPathAsync(path);
            return await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);
        }

        public bool Exists(string filename)
        {
            var file = _files.FirstOrDefault(x => x.Name == filename);
            return file != null;
        }

        private static readonly Storage _instance = new Storage();
        public static Storage Instance { get { return _instance; } }
    }
}

有什么想法吗?

【问题讨论】:

    标签: c# windows-8 microsoft-metro windows-runtime winrt-async


    【解决方案1】:

    这似乎解决了它的问题;

        public static class Storage
    {
        private static IReadOnlyList<StorageFile> _files;
    
        static Storage()
        {
            _files = GetFilesAsync("Assets").Result;
        }
    
        private async static Task<IReadOnlyList<StorageFile>> GetFilesAsync(string relativeFolderPath)
        {
            var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets").AsTask().ConfigureAwait(false);
            return await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);
        }
    
        public static bool Exists(string filename)
        {
            var file = _files.FirstOrDefault(x => x.Name == filename);
            return file != null;
        }
    }
    

    更多信息;

    【讨论】:

    • 感谢分享,它一定会帮助其他遇到同样问题的人。
    猜你喜欢
    • 1970-01-01
    • 2015-01-02
    • 2012-06-14
    • 2019-02-07
    • 2018-07-18
    • 2014-07-20
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多