【发布时间】:2014-05-04 03:58:34
【问题描述】:
我正在尝试从 BackgroundWorker 线程更新 UI 中的 BitmapImage。我对后台工作人员有足够的了解,可以对其进行一般设置,以及如何使用 ObservableCollection 从 BackgroundWorker 更新列表,但我正在努力更新图像。
当我设置时
到目前为止,它看起来像这样:
XAML:
<Image Source="{Binding ImageSource}" />
视图模型:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private BitmapImage ImageSource_;
public BitmapImage ImageSource
{
get { return ImageSource_; }
set { ImageSource_= value; NotifyPropertyChanged("ImageSource"); }
}
private BackgroundWorker UpdateImageBGW = new BackgroundWorker();
public ViewModel()
{
// this works fine
ImageSource = UpdateImage();
UpdateImageBGW.DoWork += new DoWorkEventHandler(UpdateImage_DoWork);
UpdateImageBGW.RunWorkerAsync();
}
private void UpdateImage_DoWork(object sender, DoWorkEventArgs e)
{
// this gets called fine and grabs the updated image, but setting it to
// ImageSource never updates the UI
ImageSource = UpdateImage();
}
}
【问题讨论】:
标签: c# wpf mvvm bitmap backgroundworker