重构代码 - 制作类

时间:2013-11-06 14:36:30

标签: c# oop windows-phone-7 windows-phone-8 windows-phone

我想创建一个类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
        }
    }
}

2 个答案:

答案 0 :(得分: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;});

    }
}

并在原始页面中订阅该事件,并在eventhandler中执行此操作:

void photoSelectCompleted(object sender, PhotoEventArgs e)
{
logoQrCodeImage.Source = e.Bmp;
}

答案 1 :(得分:0)

您可以构建您的类以支持Tasks,然后使用async / await事件处理程序。所以你的课程看起来像这样:

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 Package来使用async和await。只需添加一个nuget引用并搜索Async。它应该是第一个。