C# 如何修复错误:无法调用请求的方法(GetBasicPropertiesAsync)。以前对此方法的调用挂起

C# 如何修复错误:无法调用请求的方法(GetBasicPropertiesAsync)。以前对此方法的调用挂起,c#,windows-phone-8,windows-store-apps,winrt-xaml,async-await,C#,Windows Phone 8,Windows Store Apps,Winrt Xaml,Async Await,我使用这个GetBasicPropertiesAsync方法在循环中逐个获取文件属性。 但我犯了一个错误: WinRT信息:无法调用请求的方法 (GetBasicPropertiesAsync)。以前对此方法的调用挂起 并且必须在再次调用该方法之前返回 My main函数有以下代码来枚举图片库中的所有文件和文件夹 List<BaseStorage> listOfFiles = new List<BaseStorage>(); IReadOnlyList<Storag

我使用这个
GetBasicPropertiesAsync
方法在循环中逐个获取文件属性。 但我犯了一个错误:

WinRT信息:无法调用请求的方法 (GetBasicPropertiesAsync)。以前对此方法的调用挂起 并且必须在再次调用该方法之前返回

My main函数有以下代码来枚举图片库中的所有文件和文件夹

List<BaseStorage> listOfFiles = new List<BaseStorage>();
IReadOnlyList<StorageFolder> foldersList = await curFolder.MCMFolder.GetFoldersAsync();

// For each folder found ...
foreach (StorageFolder folder in foldersList)
{
    listOfFiles.Add(new Folder(folder, parents));
}

// Enumerate all files in the Pictures library.
IReadOnlyList<StorageFile> fileList = await curFolder.MCMFolder.GetFilesAsync();
// For each file found ...
foreach (StorageFile file in fileList)
{
    listOfFiles.Add(new Document(file));
}

return listOfFiles;
List listOfFiles=new List();
IReadOnlyList foldersList=wait curFolder.MCMFolder.GetFoldersAsync();
//对于找到的每个文件夹。。。
foreach(文件夹列表中的StorageFolder文件夹)
{
添加(新文件夹(文件夹,父文件夹));
}
//枚举图片库中的所有文件。
IReadOnlyList fileList=wait curFolder.MCMFolder.getfileasync();
//对于找到的每个文件。。。
foreach(文件列表中的存储文件)
{
添加(新文档(文件));
}
返回文件列表;
文件夹和文档类继承BaseStorage类

class BaseStorage
{
    public BaseStorage(IStorageItem storageItem)
    {

        this.Name = storageItem.Name;
        this.CreationDate = storageItem.DateCreated.ToString();

        setModifiedDateAndOwner(storageItem);
    }

    private async void setModifiedDateAndOwner(IStorageItem storageItem)
    {

        // await Task.Delay(500);

        Windows.Foundation.IAsyncOperation<BasicProperties> basicPropsTask = storageItem.GetBasicPropertiesAsync();

        BasicProperties _basicProps = await storageItem.GetBasicPropertiesAsync();

        this.ModifiedDate = _basicProps.DateModified.ToString();

        string fileOwnerProperty = "System.FileOwner";
        List<string> propertiesToFetch = new List<string>();
        propertiesToFetch.Add(fileOwnerProperty);

        IDictionary<string, object> props = await _basicProps.RetrievePropertiesAsync(propertiesToFetch);
        this.Owner = props[fileOwnerProperty].ToString();

        return;
    }
}

class Document
{
    public Document()
    {
        setSize();
    }

    private async void setSize()
    {
        BasicProperties _basicProps = await file.GetBasicPropertiesAsync();
        ulong fileSizeInBytes = _basicProps.Size;
    }
}
类基本存储
{
公共基础存储(IStorageItem storageItem)
{
this.Name=storageItem.Name;
this.CreationDate=storageItem.DateCreated.ToString();
SetModifiedDataAndowner(存储项);
}
私有异步void SetModifiedDataAndowner(IStorageItem storageItem)
{
//等待任务。延迟(500);
Fask.IasyCycaseBasic PROSTSTASK = SturgEngest.GETBasePrimeStaseCyc();
BasicProperties_BasicProperties=等待存储项。GetBasicProperties异步();
this.ModifiedDate=_basicrops.DateModified.ToString();
字符串fileOwnerProperty=“System.FileOwner”;
List propertiesToFetch=新列表();
添加(fileOwnerProperty);
IDictionary props=wait _basicrops.RetrievePropertiesAsync(propertiesToFetch);
this.Owner=props[fileOwnerProperty].ToString();
返回;
}
}
类文档
{
公共文件()
{
设置大小();
}
私有异步void setSize()
{
BasicProperties_BasicProperties=await file.getBasicProperties异步();
ulong fileSizeInBytes=_basicrops.Size;
}
}
这里的问题是方法
setModifiedDataAndowner
调用了
GetBasicPropertiesAsync
方法。甚至在该方法完成之前 子类-文档调用
setSize
方法,该方法再次调用
GetBasicPropertiesAsync
方法

这会导致发生异常。但是,由于线程的原因,行为不太一致


在调用基类的子类中的方法之前,如何确保基类中的方法
setModifiedDataAndOwner
已完成。

构造函数不能是
异步的,但是

由于您使用的是继承,所以不能直接使用上述文章中的工厂模式,但可以将派生类的工厂模式和基类的异步初始化模式的版本结合起来:

class BaseStorage
{
    protected BaseStorage(IStorageItem storageItem)
    {
        this.Name = storageItem.Name;
        this.CreationDate = storageItem.DateCreated.ToString();

        Initialization = setModifiedDateAndOwner(storageItem);
    }

    protected Task Initialization { get; private set; }

    private async Task setModifiedDateAndOwner(IStorageItem storageItem)
    {
        …
    }
}

class Document : BaseStorage
{
    private Document(IStorageFile storageFile)
        : base(storageFile)
    { }

    public static async Task<Document> Create(IStorageFile storageFile)
    {
        var result = new Document(storageFile);

        await result.Initialization;

        return result;
    }
}
类基本存储
{
受保护的基本存储(IStorageItem storageItem)
{
this.Name=storageItem.Name;
this.CreationDate=storageItem.DateCreated.ToString();
初始化=setModifiedDataAndowner(存储项);
}
受保护的任务初始化{get;private set;}
专用异步任务SetModifiedDataAndowner(IStorageItem storageItem)
{
…
}
}
类文档:BaseStorage
{
私人文档(IStorageFile存储文件)
:base(存储文件)
{ }
公共静态异步任务创建(IStorageFile-storageFile)
{
var结果=新文档(存储文件);
等待结果。初始化;
返回结果;
}
}
这样,在实现
Document
和从
BaseStorage
继承的其他类时必须小心,但只能正确使用这些类(必须
等待
任务
才能获得创建的实例)