Caliburn.Micro 问题:XamlParseException“无法设置未知成员”{clr-namespace:Caliburn.Micro; assembly = Caliburn.Micro}View.Model“”
我收到一个非常奇怪的 XamlParseException,我不知道为什么。
该消息是“无法设置未知成员 '{clr-namespace:Caliburn.Micro; assembly=Caliburn.Micro}View.Model'”。
在视图模型中,我有一个 ObservableCollection,我在构造函数中初始化它,如下所示:
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; }
}
这是视图:
<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>
当我在 EntityListScreenViewModel 的实例上调用 WindowManager.ShowWindow() 时,出现异常。
如果我不将实体添加到列表中(使用var list = new List
代替),我不会得到异常。
有人有什么想法吗?
更新:
我尝试将 ObservableCollection 更改为字符串类型并添加单个字符串,但没有收到异常。我怀疑 Caliburn.Micro 正在以某种方式寻找一个视图来表示 ListBox 中的实体。也许这就是正在发生的事情吗?
更新2:
我终于弄清楚到底发生了什么...ConventionManager中的DefaultItemTemplate有一些解析的Xaml正在寻找“Caliburn.Micro”程序集,但我已将代码放入与另一个程序集。更改了 Xaml,问题就消失了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这就是正在发生的事情。默认情况下,当您使用名称约定绑定列表时,Caliburn Micro 会将其解释为您绑定到 ViewModel 列表;不是实体列表。这允许您绑定到 ViewModel 列表,而无需在
ItemsControl
的ItemTemplate
中指定要使用的特定视图,最终得到的列表是特定于该 ViewModel 的视图。为了确保这种情况不会发生,您应该能够手动绑定到列表框。如果 Caliburn Micro 已经看到 ItemsSource 绑定,它将忽略该约定。
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
ItemTemplate
of theItemsControl
and 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.
我不确定,但您可以尝试将
EntityListScreenView
放入Views
命名空间中,并将EntityListScreenViewModel
放入ViewModels
命名空间中 -当然,如果您使用“标准”Bootstrapper
类。I'm not sure, but you can try putting
EntityListScreenView
inViews
namespace andEntityListScreenViewModel
inViewModels
namespace - of course if you user "standard"Bootstrapper
class.