Home > English > Developing Windows 8 – Most Recently Used List

Developing Windows 8 – Most Recently Used List

In the last post of the series, we discussed some of the challenges that I ran into using the Documents Library.  It turns out, unless I want to actually programmatically modify files in the file system, I really didn’t need the added security and license constraints of using the Documents Library.  All Windows 8 applications come with the feature of remember the last 25 most recently used files.  In the application I developed, I wanted to have the ability to keep track of two separate files and always get the most recent of each.

This is where the sample documentation and I diverged in paths.  I believe that this feature is very cool when you are only dealing with a single file but can become a little more difficult when you need to manage multiple files and have them open when your application starts.  The following code snippet demonstrates responding to a user initiated event to load a load a file:

var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().done(function (pickedFile) {
    if (pickedFile) {
        // Process picked file
    } else {
        // Canceled; no file was picked
    }
});

The code is fairly straightforward in that it just waits for the user to pick a file asynchronously and then it fires the “done” function to process the file selected. Now let’s look at what it takes to add support for the most recently used (MRU) feature of Windows 8:

var mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList.add(pickedFile, pickedFile.name);

As you can see, this one line of code is all that we need to add our recently selected file. The value returned from the add function is a unique string token that will allow you to retrieve the selected file at a later time. Let’s now look at how we can grab a file from the most recently used list.

var mruFirstToken = Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList.entries.first.token;
Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList.getFileAsync(mruFirstToken).done(
    function (retrievedFile) {
        // Process retrieved file
    },
    function (error) {
        // Handle errors
    }
);

This example grabs the first item in the most recently used list and uses the AccessCache to return the file from the file system.

The same feature that allows you to access a file system will also allow you to access folders.

If you read the documentation, you will also notice that the second argument to the add is metadata. We can use this to our advantage in that we can keep track of the file types. It is this convention that allows us to have multiple panes and get the latest one for each.

Now that we know that we can pass in metadata, we can then use this metadata as a filter against the entries list.

With the metadata value we can use the following method to pull the actual entry from the list:

function getFirstToken(metadata) {
    var mruToken;
    var mruEntries = Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList.entries;
    if (mruEntries.size > 0) {
        mruEntries.forEach(function (entry) {
            if (entry.metadata === metadata) {
                mruToken = entry.token;
            }
            // Continue processing the MRU entry
        });
    } else {
        // Handle empty MRU
    }

    return mruToken;
};

Now that we have our token, we can then read the file.

function readToken(metadata, token) {
    Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList.getFileAsync(token).done(
        function (retrievedFile) {
            // Process retrieved file
            // ...
        },
        function (error) {
            // Handle errors
            // ...
        }
    );
}

It is with this method that we are finally able to get the correct file from the most recently used list. Because I am loading multiple files in my application, I need to store the metadata for each file type. I typically store my file type information into the roamingSettings object. It is then when a user launches the application at a later time that we can reference the value stored in the roamSettings object and then get the getFirstToken method.

If you are interested in reading more on most recently used lists in Windows 8 applications, just read the following link.  I am very impressed with the power and capabilities  inherent with developing Windows 8 applications.

Hope this helps…

Advertisements
Categories: English Tags: , , , ,
  1. EC
    December 12, 2012 at 5:18 pm

    I get an error on the line var mruFirstToken = Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList.entries.first.token; I also get an error using a similar example from microsoft code. The .First.Token does not seem to work for me.
    The microsoft code is String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First.Token;
    I have included using Windows.Storage.AccessCache; & using Windows.Storage;
    The microsoft example can be found at http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh972344.aspx. I must of missed a step along the way.

    • EC
      December 12, 2012 at 6:56 pm

      Sorry, I answered my own question. The code should be
      String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First().Token; Notice the () after First.

      • December 13, 2012 at 1:55 am

        Hi EC,

        Glad you figured it out!

        Best regards,

        Matt

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: