I am getting a really weird XamlParseException, and I have no idea why.
The message is “Cannot set unknown member ‘{clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro}View.Model’.”
In the view model, I have an ObservableCollection that I’m initializing in the constructor like this:
internal class EntityListScreenViewModel : Screen
{
public EntityListScreenViewModel()
{
var list = new List<Entity>() { new Entity() { Name = "Joe" } };
this.Entities = new ObservableCollection<Entity>(list);
}
public ObservableCollection<Entity> Entities { get; set; }
}
Here’s the view:
<Window x:Class="WpfApp.EntityListScreenView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Grid>
<ListBox x:Name="Entities"/>
</Grid>
</Window>
When I call WindowManager.ShowWindow() on an instance of EntityListScreenViewModel, I get the exception.
If I do not add an Entity to the list (using var list = new List<Entity>(); instead), I do not get the exception.
Does anybody have any ideas?
Update:
I tried changing the ObservableCollection to be of type string and added a single string, and I did not get the exception. My suspicion is that Caliburn.Micro is somehow looking for a view to represent the Entity in the ListBox. Could that perhaps be what’s going on?
Update 2:
I finally figured out what was really going on… the DefaultItemTemplate in the ConventionManager had some parsed Xaml that was looking for the “Caliburn.Micro” assembly, but I had put the code in with another assembly. Changed the Xaml and the problem went away.
Yes, this is what is going on. By default when you bind a list using a name convention, Caliburn Micro interprets this as you binding to a list of ViewModels; not a list of Entities. This allows you to bind to a list of ViewModels without the need to specify the specific view to use in the
ItemTemplateof theItemsControland what you end up with is a list of views specific to that ViewModel.To ensure this doesn’t happen, you should be able to just bind manually to the ListBox. If Caliburn Micro sees an ItemsSource binding already, it will ignore the convention.