【问题标题】:The most appropriate bitmap class for the model最适合模型的位图类
【发布时间】:2011-05-25 11:06:21
【问题描述】:

我正在使用 MVVM 编写一个简单的 WPF 应用程序。 从模型中检索位图并进一步绑定数据最方便的类是什么:Bitmap、BitmapImage、BitmapSource?

public class Student
{
    public <type?> Photo
    {
        get;
    }
}

或者也许我可以使用 ViewModel 以某种方式将 Bitmap 转换为 BitmapSource?

【问题讨论】:

  • 方便是什么意思?

标签: c# wpf mvvm model bitmap


【解决方案1】:

我一直使用BitmapImage,它非常专业,并提供了可能有用的好属性和事件(例如IsDownloadingDownloadProgressDownloadCompleted)。

【讨论】:

    【解决方案2】:

    我想更灵活的方法是将照片(或任何其他位图)作为流返回。 此外,如果照片已更改,模型应触发照片更改事件,客户端应处理照片更改事件以检索一张新照片。

    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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-09
      • 2019-07-11
      • 2020-07-02
      • 2010-11-02
      • 2019-07-23
      • 1970-01-01
      • 2020-01-30
      • 2011-03-10
      相关资源
      最近更新 更多