Mobile Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

New URI schemas in Windows Phone SDK 7.1 - isostore and appdata

07.23.2011
| 14462 views |
  • submit to reddit

Yet another addition to the Mango SDK is the ability to access application data through URI schemas. The idea is there in many Microsoft assemblies, as system applications themselves are invoked internally through the app URI schema. So how exactly can the user leverage the new schemas? I am going to extend my sample from yesterday (the one describing the process of adding ringtones) and add capabilities to read content from Isolated Storage and application data.

Here is a short snippet that will let the developer download a WMA file that will later become a ringtone. For now it will be stored in the Isolated Storage.

private void button1_Click(object sender, RoutedEventArgs e)
{
    WebClient client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    client.OpenReadAsync(new Uri("http://dennisdel.com/test/coffee.wma"));
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("ringtone.wma", System.IO.FileMode.Create, file))
    {
        byte[] buffer = new byte[1024];
        while (e.Result.Read(buffer, 0, buffer.Length) > 0)
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }

    SaveRingtoneTask task = new SaveRingtoneTask();
    task.Source = new Uri("isostore:/ringtone.wma");
    task.Show();
}

Calling SaveRingtoneTask is not mandatory at the end, but I just want to show you how the isostore URI schema is referenced. Notice how I am storing the ringtone in the root of the app storage, so there are no folders considered. Ultimately, I can reference a folder, but then I will have to reference it in the isostore path as well. The same way I would do with regular files on a PC.

It is worth mentioning that you will not be able to access the isolated storage through isostore in any HttpWebRequest-like class (e.g. WebClient) - it will cause an exception because isostore is not a supported schema by default.

Bottom line - you should only use isostore to read data inside your own application.Make sure that the class you are trying to invoke and set a property to an internal URI actually supports it.

For example, isostore-based URIs are supported when:

  • Setting the source for ringtones
  • Accesing local database (SDF) files

appdata falls in the same category with isostore when it comes to determining which classes can use this schema in their URI properties. appdata is used to access data from the XAP package itself. So, if I would have a ringtone called coffee.wma that is added as content in my project, I can easily access it via:

SaveRingtoneTask task = new SaveRingtoneTask();
task.Source = new Uri("appdata:/coffee.wma");
task.Show();

Relative paths are also supported, so if a file is located inside a folder, it can still be accessed, given the correct path string is passed.

Also, the new URI schema can be used in connection strings (for applications that work with SQL CE):

“datasource=’appdata:/database.sdf’; mode=’read only’” 

Ultimately, a lot of classes across the Windows Phone SDK do not support isostore and appdata as reference points. Chances are this will be fixed in the future (remember that the SDK is still in beta).