多个图像选择 [英] Multiple Images Selection

查看:26
本文介绍了多个图像选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Windows Phone SDK 7.1/C# 在 WP7 中制作应用程序

Am making an application in WP7 using Windows Phone SDK 7.1 / C#

我正在使用照片选择器任务.但是我想选择9张图片,这里我只能选择一张图片.

Am using photo chooser task. But I want to select 9 images and here I can select only one Image.

如何一次选择 9 张图片?

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.");
        }
    }
}

}
}

推荐答案

虽然 PhotoChooserTask 将只允许用户从他们的库中选择一个图像(或者如果你设置了从他们的相机中捕获一个新的ShowCamera 为 true)另一种选择是继续允许用户迭代选择图像,并且在他们选择 9 之前不允许他们继续(我相信这是您所追求的要求?)

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...");
    }
}

在您的 XAML 中,您希望将一个图像放入 ListBoxDataTemplate 中,并显示用户当前输入的所有图像(可能有一个选项可以擦除图像并添加不同的图像).例如;

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" />

如果要求 最多 9 张图片,我更喜欢这种方法,而不是自定义的多选图片选择器,原因有几个;

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;

  1. 它为用户提供一致的用户体验 - 与他们在任何其他应用中获得的体验相同
  2. 通过使用 MediaLibrary,您的应用程序将需要 ID_CAP_MEDIALIB 功能(我相信显示为访问媒体库").
  3. 我发现这种方法比成熟的媒体选择器要少一些工作.
  1. It provides the user with a consistent user experience - the same as they'd get in any other app
  2. By making use of the MediaLibrary your application will require the ID_CAP_MEDIALIB capability (Shows up as "Access media library", I believe).
  3. I'd find this method a bit less work than a full blown media choser.

这篇关于多个图像选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆