C# 重构代码-生成类
我想制作一张班级照片,在那里我可以提交方法,因为我将在许多页面中多次使用这些方法。 那么,如何在参数中发送图像 现在我有:C# 重构代码-生成类,c#,oop,windows-phone-7,windows-phone-8,windows-phone,C#,Oop,Windows Phone 7,Windows Phone 8,Windows Phone,我想制作一张班级照片,在那里我可以提交方法,因为我将在许多页面中多次使用这些方法。 那么,如何在参数中发送图像 现在我有: PhotoChooserTask selectPhoto = null; private void chooseLogoButton_Click(object sender, RoutedEventArgs e) { selectPhoto = new PhotoChooserTask(); selectPhoto.Compl
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;
}
}
PhotoChooserTask selectPhoto=null;
private void chooseLogoButton\u单击(对象发送者,路由目标)
{
选择Photo=新建PhotoChooserTask();
selectPhoto.Completed+=新事件处理程序(selectPhoto\u Completed);
选择photo.Show();
}
void selectPhoto_已完成(对象发送方,PhotoResult e)
{
if(e.TaskResult==TaskResult.OK)
{
Show(例如ChosenPhoto.Length.ToString());
//代码以在名为myImage的图像控件中显示页面上的照片。
System.Windows.Media.Imaging.BitmapImage bmp=新系统.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
}
}
}
公共课照片
{
PhotoChooserTask selectPhoto=null;
public void chooseLogo()
{
选择Photo=新建PhotoChooserTask();
selectPhoto.Completed+=新事件处理程序(selectPhoto\u Completed);
选择photo.Show();
}
void selectPhoto_已完成(对象发送方,PhotoResult e)
{
if(e.TaskResult==TaskResult.OK)
{
Show(例如ChosenPhoto.Length.ToString());
//代码以在名为myImage的图像控件中显示页面上的照片。
System.Windows.Media.Imaging.BitmapImage bmp=新系统.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
logoQrCodeImage.Source=bmp;//错误
}
}
}
您可以提供一个回调,说明在照片完成选择后要做什么
例如,您可以将事件添加到Photo类(您还需要定义类PhotoEventHandler)
在原始页面中订阅事件,并在eventhandler中执行以下操作:
void photoSelectCompleted(object sender, PhotoEventArgs e)
{
logoQrCodeImage.Source = e.Bmp;
}
您可以构建类来支持任务,然后使用异步/等待事件处理程序。所以你的班级看起来是这样的:
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和Wait。只需添加一个nuget引用并搜索Async。这应该是第一个问题。我不明白你在问什么。你应该更清楚地陈述你的问题,我猜@Frappeer引用了//ERROR:
logoQrCodeImage.Source=bmp//错误
,因为他在Photo类中没有对logoQrCodeImage的引用。
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();
}