我正在使用MVVM编写一个简单的WPF应用程序。 什么是最方便的类检索模型和进一步的数据绑定位图Bitmap,BitmapImage,BitmapSource?模型的最合适的位图类
public class Student
{
public <type?> Photo
{
get;
}
}
或者我可以以某种方式使用ViewModel将位图转换为BitmapSource?
我正在使用MVVM编写一个简单的WPF应用程序。 什么是最方便的类检索模型和进一步的数据绑定位图Bitmap,BitmapImage,BitmapSource?模型的最合适的位图类
public class Student
{
public <type?> Photo
{
get;
}
}
或者我可以以某种方式使用ViewModel将位图转换为BitmapSource?
我想更灵活的方法是将照片(或任何其他位图)作为流返回。 此外,如果照片已更改,模型应该触发照片更改事件,客户端应处理照片更改事件以检索新照片。
public class PhotoChangedEventArgs : EventArgs
{
}
public class Student
{
public Stream GetPhoto()
{
// Implementation.
}
public event EventHandler<PhotoChangedEventArgs> OnPhotoChanged;
}
public class StudentViewModel : ViewModelBase
{
// INPC has skipped for clarity.
public Student Model
{
get;
private set;
}
public BitmapSource Photo
{
get
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = Model.Photo;
image.EndInit();
image.Freeze();
return image;
}
}
public StudentViewModel(Student student)
{
Model = student;
// Set event handler for OnPhotoChanged event.
Model.OnPhotoChanged += HandlePhotoChange;
}
void HandlePhotoChange(object sender, PhotoChangedEventArgs e)
{
// Force data binding to refresh photo.
RaisePropertyChanged("Photo");
}
}
我总是用BitmapImage
,这是相当专业的,并提供很好的属性和事件,可能是有用的(例如IsDownloading
,DownloadProgress
& DownloadCompleted
)。
你是什么意思? – 2011-05-25 11:09:41
便于数据绑定。 – 2011-05-25 11:10:29
数据绑定到什么? – 2011-05-25 11:13:29