0

我想做一个类 Photo 我可以在哪里提交方法,因为我会在很多页面中多次使用这些方法。那么如何在参数中发送我的图像?

我现在有 :

PhotoChooserTask selectPhoto = null;
    private void chooseLogoButton_Click(object sender, RoutedEventArgs e)
    {
        selectPhoto = new PhotoChooserTask();
        selectPhoto.Completed += new EventHandler<PhotoResult>(selectPhoto_Completed);
        selectPhoto.Show();
    }

    void selectPhoto_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(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);
            logoQrCodeImage.Source = bmp; 
        }
    }

我做了一张班级照片:

public class Photo
{
    PhotoChooserTask selectPhoto = null;

    public void chooseLogo()
    {
        selectPhoto = new PhotoChooserTask();
        selectPhoto.Completed += new EventHandler<PhotoResult>(selectPhoto_Completed);
        selectPhoto.Show();
    }

     void selectPhoto_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(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);
            logoQrCodeImage.Source = bmp; //ERROR
        }
    }
}
4

2 回答 2

0

当照片完成选择时,您可以提供回调。

例如,您可以将事件添加到 Photo 类(您还需要定义类 PhotoEventHandler)

    public class PhotoEventArgs
    {
        public Bitmap Bmp;
    }

    public event EventHandler<PhotoEventArgs> photoSelectCompleted;

并在方法中调用事件

 void selectPhoto_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        MessageBox.Show(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);
        if (photoSelectCompleted != null)
            photoSelectCompleted(this, new PhotoEventArgs(){ Bmp = bmp;});

    }
}

并在原始页面中订阅事件并在事件处理程序中执行以下操作:

void photoSelectCompleted(object sender, PhotoEventArgs e)
{
logoQrCodeImage.Source = e.Bmp;
}
于 2013-11-06T14:46:40.560 回答
0

您可以构建您的类以支持任务,然后使用异步/等待事件处理程序。所以你的班级看起来像这样:

public class PhotoChooser
{
    public Task<BitmapImage> ChoosePhoto()
    {
        var taskSource = new TaskCompletionSource<BitmapImage>();
        var chooser = new PhotoChooserTask();
        chooser.Completed += (s, e) =>
            {
                if (e.ChosenPhoto == null)
                {
                    taskSource.SetResult(null);
                }
                else
                {
                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(e.ChosenPhoto);
                    taskSource.SetResult(bmp);   
                }
            };
        chooser.Show();
        return taskSource.Task;
    }
}

您的事件处理程序将如下所示:

private async void ChoosePhoto_OnClick(object sender, RoutedEventArgs e)
{
    var chooser = new PhotoChooser();
    logoQrCodeImage.Source = await chooser.ChoosePhoto();
}

更新:

如果您使用的是 Windows Phone 7,您仍然可以通过添加 Async for Silverlight、.NET 4、Windows Phone NuGet 包来使用 async 和 await。只需添加一个 nuget 引用并搜索 Async。它应该是第一个。

于 2013-11-06T19:05:06.743 回答