[英]File Picker in Windows Phone 8.1
我想從 Windows Phone 8.1 的相冊中選擇一張圖片。 為此,我使用了這段代碼,但它給出了錯誤
private async void gallery_Tapped(object sender, TappedRoutedEventArgs e)
{
FileOpenPicker opener = new FileOpenPicker();
opener.ViewMode = PickerViewMode.Thumbnail;
opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
opener.FileTypeFilter.Add(".jpg");
opener.FileTypeFilter.Add(".jpeg");
opener.FileTypeFilter.Add(".png");
StorageFile file = await opener.PickSingleFileAsync();
if (file != null)
{
// We've now got the file. Do something with it.
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
MyImage.Source=bitmapImage;
}
else
{
//OutputTextBlock.Text = "The operation may have been cancelled.";
}
}
錯誤
我認為您甚至可以在您需要的頁面中處理 OnActivated 事件。 像這樣的東西
CoreApplicationView view = CoreApplication.GetCurrentView();
ImagePath=string.Empty;
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
storageFileWP = args.Files[0];
}
}
當您從選擇器中選擇文件時,將調用上述方法。 我相信它對你有幫助。
在 Windows Phone 8.1 中使用 FileOpenPicker 從圖片庫中選擇圖片。
步驟 1:在您的 Windows Phone 8.1 應用程序中添加圖片庫功能。
第 2 步:添加 File Open Picker 作為聲明。
步驟 3:向 MainPage.xaml 添加按鈕和圖像。
<Grid>
<Image Name="img"/>
<Button Content="click me" Click="Button_Click"/>
</Grid>
第四步:添加全局變量視圖。
CoreApplicationView view;
步驟 4.1 在頁面構造函數中初始化。
view = CoreApplication.GetCurrentView();
第 5 步:添加代碼以在按鈕單擊事件上調用文件打開選擇器。
private void Button_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
}
第 6 步:在 View 激活事件上將圖像設置為 MainPage。
private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
StorageFile storageFile = args.Files[0];
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
img.Source=bitmapImage;
}
}
它還允許您拍照並使用它。
使用 RoutedEventArgs 而不是 TappedRoutedEventArgs 在 wp 8.1 xaml 中單擊按鈕 不要使用 async 關鍵字
private void OpenImageFile(object sender, RoutedEventArgs e) { FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; filePicker.ViewMode = PickerViewMode.Thumbnail; // Filter to include a sample subset of file types filePicker.FileTypeFilter.Clear(); filePicker.FileTypeFilter.Add(".bmp"); filePicker.FileTypeFilter.Add(".png"); filePicker.FileTypeFilter.Add(".jpeg"); filePicker.FileTypeFilter.Add(".jpg"); filePicker.PickSingleFileAndContinue(); view.Activated += viewActivated; } private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) { FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; if (args != null) { if (args.Files.Count == 0) return; view.Activated -= viewActivated; StorageFile SelectedImageFile = args.Files[0]; } }
我認為這會有所幫助:) 謝謝
var fill = await StorageFile.GetFileFromPathAsync(selectItem.FolderPath); BitmapImage bit = new BitmapImage();
if (fill != null)
{
// We've now got the file. Do something with it.
var stream = await fill.OpenAsync(Windows.Storage.FileAccessMode.Read);
//var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
//await bitmapImage.SetSourceAsync(stream);
bit.SetSource(stream);
imgTeste.Source = bit;
pvMestre.SelectedIndex = 1;
}
else
{
//OutputTextBlock.Text = "The operation may have been cancelled.";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.