问题
Am making an application in WP7 using Windows Phone SDK 7.1 / C#
Am using photo chooser task. But I want to select 9 images and here I can select only one Image.
How can I select 9 images at a stretch?
Please Help me, here is my code:
public partial class MainPage : PhoneApplicationPage
{
PhotoChooserTask photoChoserTask;
// Constructor
public MainPage()
{
InitializeComponent();
photoChoserTask = new PhotoChooserTask();
photoChoserTask.Completed +=
new EventHandler<PhotoResult>(photoChooserTask_Completed);
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
y.Text= (e.ChosenPhoto.Length.ToString());
//Code to display the photo on the page in an image control named myImage.
//System.Windows.Media.Imaging.BitmapImage bmp =
//new System.Windows.Media.Imaging.BitmapImage();
//bmp.SetSource(e.ChosenPhoto);
//myImage.Source = bmp;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
{
try
{
photoChoserTask.Show();
}
catch (System.InvalidOperationException )
{
MessageBox.Show("An error occurred.");
}
}
}
}
}
回答1:
Whilst PhotoChooserTask
will only allow the user to select one image from their library (or capture a new one from their camera if you set ShowCamera
to true) another option would be to continue to allow the user to iteratively select images and not allow them to continue until they have selected 9 (I believe that's the requirement you're after?)
public partial class MainPage : PhoneApplicationPage {
public class SelectedPhoto : IDisposable {
public Stream Data { get; private set; }
public string Name { get; private set; }
public BitmapImage Image { get; private set; }
public SelectedPhoto(string name, Stream photo) {
Name = name;
Data = new MemoryStream();
photo.CopyTo(Data);
Image = new BitmapImage();
Image.SetSource(Data);
}
public void Dispose() {
Data.Dispose();
}
}
private List<SelectedPhoto> _selectedPhotos = new List<SelectedPhoto>();
private PhotoChooserTask photoChoserTask;
// Constructor
public MainPage() {
InitializeComponent();
photoChoserTask = new PhotoChooserTask();
photoChoserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
ProcessImages.IsEnabled = false;
ImageListBox.ItemsSource = _selectedPhotos
}
void photoChooserTask_Completed(object sender, PhotoResult e) {
if (e.TaskResult == TaskResult.OK) {
_selectedPhotos.Add(new SelectedPhoto(e.OriginalFileName, e.ChosenPhoto);
Button.IsEnabled = _selectedPhotos.Count < 9;
ProcessImages.IsEnabled = _selectedPhotos.Count == 9;
}
}
private void Button_Click(object sender, RoutedEventArgs e) {
{
try {
photoChoserTask.Show();
} catch (System.InvalidOperationException) {
MessageBox.Show("An error occurred.");
}
}
}
private void ProcessImages_Click(object sender, RoutedEventArgs e) {
MessageBox.Show("Doing something with your images... please wait...");
}
}
In your XAML rather than having a single Image you'd want to put an Image inside a ListBox
's DataTemplate
and show all of the user's currently entered images (probably with an option to erase an image and add a different image instead). Eg;
<ListBox x:Name="ImageListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Image}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="ProcessImages" Click="ProcessImages_Click" />
If the requirement is to have up to 9 images, I'd prefer this approach, over a custom multi-select image selector, for a few reasons reasons;
- It provides the user with a consistent user experience - the same as they'd get in any other app
- By making use of the
MediaLibrary
your application will require theID_CAP_MEDIALIB
capability (Shows up as "Access media library", I believe). - I'd find this method a bit less work than a full blown media choser.
回答2:
I've release a MultiPhotoChooser on CodePlex for WP8.0 (At least works on 8.0 because that's the platform I developed on.)
Feel free to download it and try it out
https://multiphotochooser.codeplex.com/
Here's how it looks like :

Please give me a thumbs up if it helped you for building the feature you need. Thanks.
回答3:
PhotoChooserTask is used to user to select one photo, To allow the user to use multiple photos, you have to create your own UI, with the list of images displayed. you can use XNA.MediaLibrary to access the existing images of devices and show. Also provide interface to user to choose multiple images.
回答4:
The PhotoChooserTask is meant to let the user select only one photo. I guess you'll have to create a Page or Control in which you show all available photos / pictures and make them multi-selectable with a CheckBox or something and return that result to your code.
来源:https://stackoverflow.com/questions/9614331/multiple-images-selection