WinRT app guide: Step 15: Exporting data as a text file
To read the other steps in the step by step guide for creating a Metro/ WinRT application go here
Welcome :) We are currently at step 15 in our step by step guide for creating a Windows Store Application- if this is the first post you’ve seen in this series I would recommend that you have a look at the other posts if you want to code along. Last time we added layout awareness and handled the different modes, and today we want to let the user export the data for the study activites.
.
You might have noticed that there was an export button in the last tutorial. We want the user to be able to export the study activities to a text file, so we have added a button for that function.
The user clicks on the button, a generic filepicker windows opens set to the documents folder and available documents limited to documents with the .txt extension. The user can select an existing file and overwrite it, or create a new one.
.
.
Since the user is in full control we don’t need to set this as a capability in the app manifest. If we were to automatically save to a file on the users system (not local in the app) we would have to declare this in the app manifest.
.
Working with files throught the filepicker is just as easy as with WPF and SL, but a bit different. You create a FileSavePicker, and set a startup location and filetype accepted, check if a file was selected and try to write to that file. Id something was to go wrong, we notify the user and let them try again, if no file is selected we notify.
.
Code for the filepicker:
[sourcecode language=“csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using Mihsp.Model;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
namespace Mihsp.Common
{
public static class FileHelper
{
public static async void SaveAsync(IEnumerable userActivities)
{
var savePicker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
};
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
try
{
await FileIO.WriteLinesAsync(file, FormatText(userActivities));
}
catch
{
DisplayMessage("Something went wrong, please try again");
}
else
{
DisplayMessage("No file selected, please try again");
}
}
static internal IEnumerable<string> FormatText(IEnumerable<StudyActivity> userActivities)
{
return
new List<string>(
userActivities.Select(
activity =>
string.Format(
"\"{0}\"\r\n{1}\r\n------Comment------\r\n{2}\r\n-------------------\r\nTime est. :\t {3}\r\nTime spent:\t {4}\r\nWorth:\t\t {5}\r\nImportance:\t {6}\r\nUrgency:\t {7}\r\n\r\n",
activity.Title,
activity.CategoryName,
activity.Comment,
activity.TimeEstimate,
activity.TimeSpent,
activity.WorthScore,
activity.ImportanceScore,
activity.UrgencyScore)).ToList());
}
static async void DisplayMessage(string message)
{
var messageDialog = new MessageDialog(message);
await messageDialog.ShowAsync();
}
}
}
[/sourcecode]
And code behind:
[sourcecode language=“csharp”]
private void Export_Click(object sender, RoutedEventArgs e)
{
FileHelper.SaveAsync(_allEntries);
}
[/sourcecode]
Rather easy, and a very neat feature- I know how frustrated I get when I can’t export data. And wouldn’t it be nice if we implemented a share contract in the app so the list of activities can be shared? That is what we will be doing next time!
Comments
Last modified on 2012-11-14