Windows手机,挑文件中使用PickSingleFileAndContinue或PickMultipleFilesAndContinue [英] Windows Phone, pick file using PickSingleFileAndContinue or PickMultipleFilesAndContinue
问题描述
我被困试图实施文件选择Windows Phone的应用程序。我需要使用 FileOpenPicker
来选择库文件。我没有得到它是如何工作的。这里是我的代码:
I got stuck trying to implementing file picker for windows phone app. I need to choose files from gallery using FileOpenPicker
. I didn't get how it works. Here is my code:
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();
}
那么,下一步该做什么?我想我需要获得一个文件对象或东西。
So, what to do next? I guess I need to get a file object or something.
我发现此链接。这就是自定义类 ContinuationManager
实施msdn的解释。该解决方案看起来奇怪的和丑陋的。我不知道这是否是最好的之一。请帮助!
I found this link. It is msdn explanation where custom class ContinuationManager
is implemented. This solution looks weird and ugly. I am not sure if it is the best one. Please help!
推荐答案
PickAndContinue是的唯一方法,将工作在Windows Phone 8.1。这不是太奇怪和丑陋,在这里不用一个简单的例子没有的 ContinuationManager 的:
PickAndContinue is the only method that would work on Windows Phone 8.1. It's not so weird and ugly, here goes a simple example without ContinuationManager:
让我们假设你要挑一个的 .JPG 的文件,可以使用FileOpenPicker:
Let's assume that you want to pick a .jpg file, you use FileOpenPicker:
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
picker.ContinuationData.Add("keyParameter", "Parameter"); // some data which you can pass
picker.PickSingleFileAndContinue();
一旦运行 PickSingleFileAndContinue();
,你的应用程序是停用。当您完成选择一个文件,然后 OnActivated
事件被触发,在那里你可以读取该文件()你已经选择:
Once you run PickSingleFileAndContinue();
, your app is deactivated. When you finish picking a file, then OnActivated
event is fired, where you can read the file(s) you have picked:
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被终止(例如资源很少)。
Note that when you run file picker, your app is deactivated and in some rare situations it can be terminated by OS (little resources for example).
的 ContinuationManager 只是一个的帮助的,应该帮助做一些事情变得更加容易。当然,您可以实现简单的情况下,自己的行为。
The ContinuationManager is only a helper that should help to make some things easier. Of course, you can implement your own behaviour for simpler cases.
这篇关于Windows手机,挑文件中使用PickSingleFileAndContinue或PickMultipleFilesAndContinue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!