Windows Phone,使用PickSingleFileAndContinue或PickMultipleFilesAndContinue选择文件

我试图为Windows手机应用程序实现文件选择器。 我需要使用FileOpenPicker从库中选择文件。 我没弄明白它是如何运作的。 这是我的代码:

 private readonly FileOpenPicker photoPicker = new FileOpenPicker(); // This is a constructor public MainPage() { //  photoPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; photoPicker.FileTypeFilter.Add(".jpg"); } // I have button on the UI. On click, app shows picker where I can choose a file private void bChoosePhoto_OnClick(object sender, RoutedEventArgs e) { photoPicker.PickMultipleFilesAndContinue(); } 

那么,接下来该做什么? 我想我需要获取文件对象或其他东西。

我找到了这个链接 。 这是msdn解释,其中实现了自定义类ContinuationManager 。 这个解决方案看起来很怪异和丑陋。 我不确定它是否是最好的。 请帮忙!

PickAndContinue是唯一适用于Windows Phone 8.1的方法。 它不是那么奇怪和丑陋,这是一个没有ContinuationManager的简单例子:

假设你想要选择.jpg文件,你使用FileOpenPicker:

 FileOpenPicker picker = new FileOpenPicker(); picker.FileTypeFilter.Add(".jpg"); picker.ContinuationData.Add("keyParameter", "Parameter"); // some data which you can pass picker.PickSingleFileAndContinue(); 

运行PickSingleFileAndContinue(); ,您的应用已停用。 完成选择文件后,会触发OnActivated事件,您可以在其中读取您选择的文件:

 protected async override void OnActivated(IActivatedEventArgs args) { var continuationEventArgs = args as IContinuationActivatedEventArgs; if (continuationEventArgs != null) { switch (continuationEventArgs.Kind) { case ActivationKind.PickFileContinuation: FileOpenPickerContinuationEventArgs arguments = continuationEventArgs as FileOpenPickerContinuationEventArgs; string passedData = (string)arguments.ContinuationData["keyParameter"]; StorageFile file = arguments.Files.FirstOrDefault(); // your picked file // do what you want break; // rest of the code - other continuation, window activation etc. 

请注意,当您运行文件选择器时,您的应用程序将被停用,并且在极少数情况下,它可以由OS终止(例如,很少的资源)。

ContinuationManager只是一个帮助 ,使一些事情变得更容易。 当然,您可以针对更简单的情况实现自己的行为。