我正在使用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