跟踪最近使用的文件和文件夹Track recently used files and folders

重要的 APIImportant APIs

通过将用户经常访问的文件添加到你的应用的最近使用列表 (MRU) 中来跟踪这些文件。Track files that your user accesses frequently by adding them to your app's most recently used list (MRU). 该平台会为你管理 MRU,它会根据各个项的上次访问时间对它们进行排序,并在列表中的项超过 25 个的限制时删除最旧的项。The platform manages the MRU for you by sorting items based on when they were last accessed, and by removing the oldest item when the list's 25-item limit is reached. 所有应用都有其各自的 MRU。All apps have their own MRU.

你的应用的 MRU 由 StorageItemMostRecentlyUsedList 类表示,你可从静态 StorageApplicationPermissions.MostRecentlyUsedList 属性获取该类。Your app's MRU is represented by the StorageItemMostRecentlyUsedList class, which you obtain from the static StorageApplicationPermissions.MostRecentlyUsedList property. MRU 项存储为 IStorageItem 对象,这意味着 StorageFile 对象(代表文件)和 StorageFolder 对象(代表文件夹)都可以添加到 MRU 中。MRU items are stored as IStorageItem objects, so both StorageFile objects (which represent files) and StorageFolder objects (which represent folders) can be added to the MRU.

备注

 有关完整示例,请参阅文件选取器示例文件访问示例 For complete samples, see the File picker sample and the File access sample.

必备条件Prerequisites

向 MRU 中添加选取的文件Add a picked file to the MRU

  • 用户选取的文件通常是他们重复返回的文件。The files that your user picks are often files that they return to repeatedly. 因此,当选取文件时,请考虑将选取的文件添加到你的应用的 MRU。So consider adding picked files to your app's MRU as soon as they are picked. 操作方法如下。Here's how.

    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
    
    var mru = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
    string mruToken = mru.Add(file, "profile pic");
    

    将重载 StorageItemMostRecentlyUsedList.AddStorageItemMostRecentlyUsedList.Add is overloaded. 此示例中使用了 Add(IStorageItem, String) ,这样便可将元数据与文件关联。In the example, we use Add(IStorageItem, String) so that we can associate metadata with the file. 设置元数据可以记录项目的用途,例如“用户头像”。Setting metadata lets you record the item's purpose, for example "profile pic". 你还可以通过调用 Add(IStorageItem) 将文件添加到不包含元数据的 MRU 中。You can also add the file to the MRU without metadata by calling Add(IStorageItem). 当你向 MRU 中添加项时,该方法会返回一个唯一标识的字符串(称为令牌),用于检索该项。When you add an item to the MRU, the method returns a uniquely identifying string, called a token, which is used to retrieve the item.

提示

需要使用该令牌从 MRU 中检索项,因此请将它保留在某处。You'll need the token to retrieve an item from the MRU, so persist it somewhere. 有关应用数据的详细信息,请参阅管理应用程序数据For more info about app data, see Managing application data.

使用令牌从 MRU 检索项Use a token to retrieve an item from the MRU

使用最适合希望检索的项的检索方法。Use the retrieval method most appropriate for the item you want to retrieve.

这里介绍了如何取回我们刚刚添加的文件。Here's how to get back the file we just added.

StorageFile retrievedFile = await mru.GetFileAsync(mruToken);

这里介绍了如何进行迭代以获取令牌,然后获取项。Here's how to iterate all the entries to get tokens and then items.

foreach (Windows.Storage.AccessCache.AccessListEntry entry in mru.Entries)
{
    string mruToken = entry.Token;
    string mruMetadata = entry.Metadata;
    Windows.Storage.IStorageItem item = await mru.GetItemAsync(mruToken);
    // The type of item will tell you whether it's a file or a folder.
}

使用 AccessListEntryView 可以迭代 MRU 中的条目。The AccessListEntryView lets you iterate entries in the MRU. 这些条目是包含项的令牌和元数据的 AccessListEntry 结构。These entries are AccessListEntry structures that contain the token and metadata for an item.

当 MRU 已满时从中删除项Removing items from the MRU when it's full

当达到 MRU 的 25 项这一限制并尝试添加新项时,会自动删除最早访问的项。When the MRU's 25-item limit is reached and you try to add a new item, the item that was accessed the longest time ago is automatically removed. 因此,添加新项之前无需删除任何项。So, you never need to remove an item before you add a new one.

未来访问列表Future-access list

除了 MRU,你的应用还具有一个未来访问列表。As well as an MRU, your app also has a future-access list. 通过选择文件和文件夹,你的用户向你的应用授予访问项的权限(若未授权可能无法访问)。By picking files and folders, your user grants your app permission to access items that might not be accessible otherwise. 如果将这些项添加到你的未来访问列表,然后当你的应用需要以后重新访问这些项时将保留该权限。If you add these items to your future-access list then you'll retain that permission when your app wants to access those items again later. 你的应用的未来访问列表由 StorageItemAccessList 类表示,你能从静态 StorageApplicationPermissions.FutureAccessList 属性获取该类。Your app's future-access list is represented by the StorageItemAccessList class, which you obtain from the static StorageApplicationPermissions.FutureAccessList property.

当用户选取一个项时,请考虑将它添加到你的未来访问列表以及 MRU 中。When a user picks an item, consider adding it to your future-access list as well as your MRU.

  • FutureAccessList 最多可保留 1000 个项。The FutureAccessList can hold up to 1000 items. 请记住:它可以保留文件夹以及文件,所以有很多文件夹。Remember: it can hold folders as well as files, so that's a lot of folders.
  • 平台永远不会为你从 FutureAccessList 删除项。The platform never removes items from the FutureAccessList for you. 当你达到 1000 个项的限制时,你就无法添加另一个项,直到你采用 Remove 方法腾出空间。When you reach the 1000-item limit, you can't add another until you make room with the Remove method.