Caliburn.Micro 问题:XamlParseException“无法设置未知成员”{clr-namespace:Caliburn.Micro; assembly = Caliburn.Micro}View.Model“”

发布于 2024-11-07 06:07:17 字数 1412 浏览 2 评论 0原文

我收到一个非常奇怪的 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别把无礼当个性 2024-11-14 06:07:17

我怀疑 Caliburn.Micro 是
以某种方式寻找一个观点
代表列表框中的实体

是的,这就是正在发生的事情。默认情况下,当您使用名称约定绑定列表时,Caliburn Micro 会将其解释为您绑定到 ViewModel 列表;不是实体列表。这允许您绑定到 ViewModel 列表,而无需在 ItemsControlItemTemplate 中指定要使用的特定视图,最终得到的列表是特定于该 ViewModel 的视图。

为了确保这种情况不会发生,您应该能够手动绑定到列表框。如果 Caliburn Micro 已经看到 ItemsSource 绑定,它将忽略该约定。

<ListBox x:Name="Entities" ItemsSource="{Binding Entities}"/>

My suspicion is that Caliburn.Micro is
somehow looking for a view to
represent the Entity in the ListBox

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 the ItemsControl 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.

<ListBox x:Name="Entities" ItemsSource="{Binding Entities}"/>
在巴黎塔顶看东京樱花 2024-11-14 06:07:17

我不确定,但您可以尝试将 EntityListScreenView 放入 Views 命名空间中,并将 EntityListScreenViewModel 放入 ViewModels 命名空间中 -当然,如果您使用“标准”Bootstrapper 类。

I'm not sure, but you can try putting EntityListScreenView in Views namespace and EntityListScreenViewModel in ViewModels namespace - of course if you user "standard" Bootstrapper class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文