我从视图模型类更新列表框有问题。我使用Caliburn Micro框架。我的情况在这里:
我在listbox上绑定了bindableCollection类型的属性:
来自视图模型的代码:
private BindableCollection<UserInfo> _friends;
public BindableCollection<UserInfo> Friends
{
get { return _friends; }
set
{
_friends= value;
NotifyOfPropertyChange(()=>Friends);
}
}
在视图模型中,我创建假服务方法,将新的新数据作为List返回,并使用此数据更新属性Friends,它们在列表框上绑定。
我每隔3秒在调度员计时器滴答事件中调用假服务方法。
private static UserInfo FakeUser()
{
var user = new UserInfo
{
Age = "16",
Emphasis = true,
IdUser = "11542",
IsBlocked = false,
IsFriend = true,
LocationInfo = new Location
{
CityName = "TN",
IdCity = 123456,
IdRegion = 1246,
RegionName = "TN",
},
StatusInfo = new Status
{
IdChat = 12,
IsLogged = true,
LastLogin = "153151",
IsChating = true,
RoomName = "Car",
},
ProjectStatusInfo = new ProjectStatus(),
IsIamFriend = true,
PlusInfo = new Plus(),
ProfilePhoto = new BitmapImage(new Uri("http://pokec.azet.sk/vanes90?i9=1f104a294997", UriKind.RelativeOrAbsolute))
};
return user;
}
private static IEnumerable<UserInfo> GetFakeFriends()
{
var list = new List<UserInfo>();
for (int i = 0; i < 20; i++)
{
list.Add(FakeUser());
}
return list;
}
private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
{
if (_isExecuting)
return;
_isExecuting = true;
new System.Threading.Tasks.Task(() =>
{
var freshFriends = GetFakeFriends();
Execute.OnUIThread((System.Action)(() =>
{
Friends.Clear();
foreach (var freshFriend in freshFriends)
{
Friends.Add(freshFriend);
}
}));
}).Start();
_isExecuting = false;
}
}
如果我没有在列表框上应用任何样式,那么效果很好。
查看:
<Grid>
<ListBox Name="Friends"
Grid.Row="2"
Margin="4,4,4,4">
</ListBox>
</Grid>
如果我应用一些样式,我在列表框上的UserInfo中绑定属性ProfilePhoto(typeof BitmapeImage)。
风格在这里:
<Style x:Key="friendsListStyle" TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid Name="RootLayout">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>
<Image Margin="4,4,4,2" Source="{Binding Path=ProfilePhoto}" Grid.Column="0"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
我收到此错误:
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)
如果我在listbox / listbox项目上创建另一个样式,我只绑定字符串或bool属性,它可以正常工作。
我只有在绑定bitmapImage属性时才有问题。
BitmapImage属性为init:
ProfilePhoto = new BitmapImage(new Uri("http://pokec.azet.sk/vanes90?i9=1f104a294997", UriKind.RelativeOrAbsolute))
URI是图片的url或文件的路径。
有什么问题?感谢您的帮助和建议。
样式很好,只有当我不在另一个线程中使用方法调用刷新数据时它才有效。
答案 0 :(得分:71)
如果您在UI线程以外的任何线程上创建BitmapImage
,这将解释此问题。您可以冻结BitmapImage
以确保可以从任何线程访问它:
var bitmapImage = new BitmapImage(...);
bitmapImage.Freeze();