簡體   English   中英

OpenFilePicker在Windows Phone 8上不起作用(不支持指定的方法)

[英]OpenFilePicker not working on Windows Phone 8 (Specified method is not supported)

我正在嘗試使用以下方法選擇一個文件:

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    try
    {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");

            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                // Application now has read/write access to the picked file
                txt.Text = "Picked file: " + file.Name;
            }
            else
            {
                txt.Text = "Operation cancelled.";
            }

    }
    catch (Exception exception)
    {
        txt.Text = exception.Message;
    }
}

...但是它拋出一個異常:“不支持指定的方法。”

我復制並粘貼了Windows Phone 8文檔中的代碼。 他們的樣本均無效。 我以為也許我缺少文檔功能/合同或其他功能,但是它們甚至在VS for Phone應用程序中都不存在。

為什么不起作用?

我已經將其跟蹤到嘗試的第一行:

FileOpenPicker openPicker = new FileOpenPicker(); // this is the line the exception is thrown on.

根據MSDN論壇,我所假設的答案是MS員工( 在此 ):

我們目前不支持選擇照片以外的文件或從其他Store應用程序中選擇文件。

因此,似乎您被困在PhotoChooserTask而不是FileOpenPicker

這確實有效,但僅適用於Windows Phone 8.1(Windows Phone),而不適用於Windows Phone 8.0 / 8.1(Windows Phone Silverlight)。

這是代碼:

FileOpenPicker singleFilePicker = new FileOpenPicker();
        singleFilePicker.ViewMode = PickerViewMode.Thumbnail;
        singleFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        singleFilePicker.FileTypeFilter.Add(".jpg");
        singleFilePicker.FileTypeFilter.Add(".jpeg");
        singleFilePicker.FileTypeFilter.Add(".png");
        singleFilePicker.PickSingleFileAndContinue();

添加此方法來處理所選的照片:

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
    {
        if (args.Files.Count > 0)
        {
            var userChosenbPhoto = args.Files[0].Name;
        }
        else
        {
            //user canceled picker
        }
    }

您也可以抓取多個文件。

最后但最重要的是,您需要在項目中添加延續管理器類。 從選擇器返回時,這將管理應用程序的重新激活。 轉到本文檔以了解如何將ContinuationManager添加到項目中(很抱歉,沒有鏈接,請在此處輸入太多信息)。

您只能從本機應用程序(例如Direct3D的應用程序)中使用FileOpenPicker

您可以改用PhotoChooserTask從“圖片中心”中選擇圖片。

根據文檔,提到:支持的最低電話:不支持

檢查此鏈接以獲取詳細信息http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/br207852.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM