Wpf XAML bind BitmapImage ViewModel属性

Wpf XAML bind BitmapImage ViewModel属性,wpf,multithreading,dispatcher,bitmapimage,Wpf,Multithreading,Dispatcher,Bitmapimage,我无法从视图模型类更新列表框。我使用Caliburn微框架。我的设想如下: 我在listbox上绑定类型为bindableCollection的属性: 视图模型中的代码: private BindableCollection<UserInfo> _friends; public BindableCollection<UserInfo> Friends { get { return _friends; } set { _friend

我无法从视图模型类更新列表框。我使用Caliburn微框架。我的设想如下:

我在listbox上绑定类型为bindableCollection的属性:

视图模型中的代码:

private BindableCollection<UserInfo> _friends;

public BindableCollection<UserInfo> Friends
{
    get { return _friends; }
    set
    {
        _friends= value;
        NotifyOfPropertyChange(()=>Friends);
    }
}
<Grid>
    <ListBox Name="Friends"
             Grid.Row="2" 
             Margin="4,4,4,4">
    </ListBox>
</Grid>
如果我在listbox/listbox项上创建另一个样式,其中我只绑定字符串或bool属性,那么效果很好

我只有在绑定bitmapImage属性时才遇到问题

BitmapImage属性初始化为:

ProfilePhoto = new BitmapImage(new Uri("http://pokec.azet.sk/vanes90?i9=1f104a294997", UriKind.RelativeOrAbsolute))
URI是图片的url或文件的路径

怎么了?谢谢你的帮助和建议


样式很好,只有当我不在另一个线程中使用方法调用刷新数据时,它才能工作。

如果您在UI线程以外的任何线程上创建
位图图像,这将解释此问题。您可以冻结
位图图像
,以确保可以从任何线程访问它:

var bitmapImage = new BitmapImage(...);
bitmapImage.Freeze();

伟大的您是如何发现的?因为
BitmapImage
继承自
System.Windows.Freezable
-“冻结的Freezable也可以跨线程共享,而未冻结的Freezable则不能。”-
Must create DependencySource on same Thread as the DependencyObject.

   at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri)
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
   at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
   at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
   at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
   at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
   at System.Windows.FrameworkElement.ApplyTemplate()
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.Border.MeasureOverride(Size constraint)
ProfilePhoto = new BitmapImage(new Uri("http://pokec.azet.sk/vanes90?i9=1f104a294997", UriKind.RelativeOrAbsolute))
var bitmapImage = new BitmapImage(...);
bitmapImage.Freeze();