Home > Uncategorized > FileOpenPicker, Windows Phone 8.1

FileOpenPicker, Windows Phone 8.1

One new features of Windows Phone 8.1 is the FileOpenPicker, which allows you select any file from the phone, and even from skydrive (ahem, OneDrive).

I tried following one of the examples on-line for how to use the FileOpenPicker, but as soon as I tried to
override OnActivated(IActivatedEventArgs e), It reported no suitable method to override. Which confused me
immensely, but I learned after a bit of messing about that Windows Phone 8.1 apps come in two flavours,
XAML and Silverlight. If you upgrade / retarget a Windows Phone 8 app, you get a Silverlight WP8 app, whereas
if you create one from scratch, you get a XAML based one, and they are VERY VERY different. This
article refers to the SILVERLIGHT flavour, not the XAML version.

Also, a very quirky thing about the FileOpenPicker, is that you’d expect an event or delegate to callback
when the operation has completed. But in fact what happens is that your application is suspended when the
FileOpenPicker opens, and is resumed when the FileOpenPicker closes. This is all to save device memory.

So, what you need to do, is create a handler for “ContractActivated” in App.xaml.cs as follows:

In App.xaml.cs
(Top level)
public FileOpenPickerContinuationEventArgs FilePickerContinuationArgs { get; set; }

(In InitializePhonrApplication)
PhoneApplicationService.Current.ContractActivated += Application_ContractActivated;

// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_ContractActivated(object sender, IActivatedEventArgs e)
{
var filePickerContinuationArgs = e as FileOpenPickerContinuationEventArgs;
if (filePickerContinuationArgs != null)
{
this.FilePickerContinuationArgs = filePickerContinuationArgs;
}
}

Then in your page, you add a button called btnSelectFile and kick off the FileOpenPicker as follows

In [YourPage].xaml.cs

private void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(“*”);
openPicker.PickSingleFileAndContinue();
}

In order to handle callback from the FileOpenPicker once the app resumes, then you have to override the OnNavigatedTo method, and check to see if the FilePickerContinuationArgs field has been populated.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
var app = App.Current as App;
if (app.FilePickerContinuationArgs != null)
{
this.ContinueFileOpenPicker(app.FilePickerContinuationArgs);
}
}

This then calls some code to pop up a messagebox with the name of the file, and copy the file to local storage for further
processing.

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
MessageBox.Show(“Picked file: ” + args.Files[0].Name);
CopyPickedFile(args.Files[0]);
}

private async void CopyPickedFile(StorageFile file)
{
await file.CopyAsync(ApplicationData.Current.LocalFolder, file.Name, NameCollisionOption.ReplaceExisting);
// now the picked file is in local storage
}

All this code is used in the Windows Phone 8.1 version of http://www.printfromWindowsPhone.com and the app
can be downloaded from Windows Phone Store http://www.windowsphone.com/en-gb/store/app/print/da1074a1-8fbe-4f87-9592-52b2d67d966d

Advertisement
Categories: Uncategorized
  1. zrinA
    November 3, 2014 at 9:51 am

    PhoneApplicationService is not supported in windowsphone8.1
    so how to solve this issue?

    Like

  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 )

Connecting to %s

%d bloggers like this: