0

我正在尝试从 Windows 商店应用程序的“我的图片”文件夹中创建文件夹结构。但我似乎无法通过第一级。

我使用以下代码创建我的第一级文件夹:

IAsyncOperation<StorageFolder> appFolder = Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync("AppPhotos");

if (appFolder==null)
{
    //Create folder
    appFolder = Windows.Storage.KnownFolders.PicturesLibrary.CreateFolderAsync("AppPhotos");
}

现在我想在这个调用 Level1 下创建另一个文件夹。

我期望能够做到以下几点:

appFolder.CreateFolderAsync("Level1");

但是我的 appFolder 没有 CreateFolderAsync 方法。

那么,如何创建该文件夹,然后如何选择它?

提前致谢

我正在使用 Visual Studio 2012、C#4.5、XAML,并且正在编写一个 Windows 商店应用程序。

4

2 回答 2

7

You seem to have missed the async/await revolution there and assume the operation to get a folder is a folder. This should work:

var appFolder = await Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync("AppPhotos");

if (appFolder == null)
{
    //Create folder
    appFolder = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFolderAsync("AppPhotos");
}

You also need to add the async keyword in the method signature wherever the above code is located and then also if your method returns a value of type T - change it to return Task<T>, so for method signature:

private void MyMethod()

you would change it to

private async Task MyMethod()

and if your current signature is

private bool MyMethod()

you would need to do

private async Task<bool> MyMethod()

Finally in that last case - you would need to also change your calls from

var myValue = MyMethod();

to

var myValue = await MyMethod();

etc. marking all methods that make calls that await other methods with the async keyword.

于 2013-03-06T22:24:19.317 回答
-1

在图片库中创建文件夹和子文件夹:

  StorageFolder appFolder;
  try
    {
       appFolder = await  Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync("AppPhotos");

    }
  catch (Exception ex)
    {

      appFolder = null;

     }
 if (appFolder == null)
   {

   //Create folder
    appFolder = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFolderAsync("AppPhotos");

    }

    //Create sub-folder
   await appFolder.CreateFolderAsync("level1");
于 2015-04-22T06:08:07.030 回答