在Windows Store应用程序的“我的图片”中创建子文件夹 [英] Create sub folders in My Pictures from Windows Store application
问题描述
我正在尝试通过Windows应用商店应用在我的图片文件夹中创建文件夹结构。但我似乎无法通过第一级。
I'm trying to create a folder structure within My Pictures folder from a Windows store app. But I can't seem to get pass the first level.
我使用以下代码创建第一级文件夹:
I create my first level folder using the following code:
IAsyncOperation<StorageFolder> appFolder = Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync("AppPhotos");
if (appFolder==null)
{
//Create folder
appFolder = Windows.Storage.KnownFolders.PicturesLibrary.CreateFolderAsync("AppPhotos");
}
现在,我想在此调用Level1下创建另一个文件夹。
Now I want to create another folder below this call Level1.
我期望能够执行以下操作:
I was expecting to be able to do the following:
appFolder.CreateFolderAsync("Level1");
但是我的appFolder中没有CreateFolderAsync方法。
But I don't have a CreateFolderAsync method from my appFolder.
那么,如何创建该文件夹,然后再选择它呢?
So, how can I create that Folder and then how would I then select it?
预先感谢
我正在使用Visual Studio 2012,C#4.5,XAML,并且正在编写Windows商店应用。
I'm using Visual Studio 2012, C#4.5, XAML and I'm writing a windows store app.
推荐答案
您似乎错过了那里的异步/等待革命,并假设获取文件夹的操作是一个文件夹。这应该起作用:
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");
}
您还需要添加 async $无论上面的代码位于何处,方法签名中的c $ c>关键字,然后如果您的方法返回的值类型为
T
,则将其更改为返回 Task< T>
,因此对于方法签名:
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()
您将其更改为
private async Task MyMethod()
,如果您当前的签名是
private bool MyMethod()
您需要做
private async Task<bool> MyMethod()
最后一种情况-您还需要更改来自的呼叫
Finally in that last case - you would need to also change your calls from
var myValue = MyMethod();
到
var myValue = await MyMethod();
等。用 async
关键字标记所有等待其他方法的调用的方法。
etc. marking all methods that make calls that await other methods with the async
keyword.
这篇关于在Windows Store应用程序的“我的图片”中创建子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!