XAML - 集合初始值设定项中的静态资源

发布于 2024-07-09 22:22:22 字数 3664 浏览 8 评论 0原文

我想添加一个已在 XAML 中实例化一次的模型对象,并将其添加到两个不同的集合(在 xaml 中)。

以下代码在 Blend 的设计时渲染良好,但在运行时出现以下错误:

For "Post1"
类型“WpfBlog.Models.Tag”的对象无法转换为类型“System.Collections.ObjectModel.ObservableCollection`1[WpfBlog.Models.Tag]”。 标记文件“WpfBlog;component/Admin.xaml”第 XX 行位置 YY 中的对象“WpfBlog.Admin”出错。

如果我注释掉“Post1”,我会在“Post2”上收到此错误
无法将元素添加到属性“Tags”,因为如果该属性使用显式集合标记,则该属性只能有一个子元素。 标记文件“WpfBlog;component/Admin.xaml”行 AA 位置 BB 中的对象“System.Windows.StaticResourceExtension”出错。

<Window.Resources>
    <model:Tag x:Key="TDD" Name="TDD" ForeColor="Black" BackColor="White" />
    <model:Tag x:Key="Agile" Name="Agile" ForeColor="White" BackColor="Black" />
    <model:Tag x:Key="Waterfail" Name="Waterfail" ForeColor="Red" BackColor="White" />
</Window.Resources>
<Window.DataContext>
    <local:AdminViewModel>
        <local:AdminViewModel.AllTags>
            <StaticResource ResourceKey="TDD"/>
            <StaticResource ResourceKey="Agile"/>
            <StaticResource ResourceKey="Waterfail"/>
        </local:AdminViewModel.AllTags>
        <local:AdminViewModel.Posts>
            <local:PostViewModel Title="Post1">
                <local:PostViewModel.Tags>
                    <StaticResource ResourceKey="TDD" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
            <local:PostViewModel Title="Post2">
                <local:PostViewModel.Tags>
                    <StaticResource ResourceKey="TDD" />
                    <StaticResource ResourceKey="Agile" />
                    <StaticResource ResourceKey="Waterfail" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
        </local:AdminViewModel.Posts>
    </local:AdminViewModel>
<Window.DataContext>

以下代码编译并运行良好,但创建了两个名为“TDD”的标签,因此如果我尝试重命名该标签,我必须对所有帖子执行此操作,而不仅仅是一个 Tag 对象。

<Window.Resources>
    <model:Tag x:Key="TDD" Name="TDD" ForeColor="Black" BackColor="White" />
    <model:Tag x:Key="Agile" Name="Agile" ForeColor="White" BackColor="Black" />
    <model:Tag x:Key="Waterfail" Name="Waterfail" ForeColor="Red" BackColor="White" />
</Window.Resources>
<Window.DataContext>
    <local:AdminViewModel>
        <local:AdminViewModel.AllTags>
            <StaticResource ResourceKey="TDD"/>
            <StaticResource ResourceKey="Agile"/>
            <StaticResource ResourceKey="Waterfail"/>
        </local:AdminViewModel.AllTags>
        <local:AdminViewModel.Posts>
            <local:PostViewModel Title="Post1">
                <local:PostViewModel.Tags>
                    <model:Tag Name="TDD" ForeColor="Black" BackColor="White" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
            <local:PostViewModel Title="Post2">
                <local:PostViewModel.Tags>
                    <model:Tag Name="TDD" ForeColor="Black" BackColor="White" />
                    <model:Tag Name="Agile" ForeColor="White" BackColor="Black" />
                    <model:Tag Name="Waterfail" ForeColor="Red" BackColor="White" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
        </local:AdminViewModel.Posts>
    </local:AdminViewModel>
<Window.DataContext>

有任何想法吗? 如果 Blend 没有正确渲染它,我可以忽略它并解决它,但它确实如此!

I want to add a single model object that has been instantiated once in XAML, and add it to two different collections (in xaml).

The following code renders fine in Blend's Design Time, but I get the following errors at run time:

For "Post1"
Object of type 'WpfBlog.Models.Tag' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection`1[WpfBlog.Models.Tag]'. Error at object 'WpfBlog.Admin' in markup file 'WpfBlog;component/Admin.xaml' Line XX Position YY.

If I comment out "Post1", I'll get this error on "Post2"
Cannot add element to property 'Tags', because the property can have only one child element if it uses an explicit collection tag. Error at object 'System.Windows.StaticResourceExtension' in markup file 'WpfBlog;component/Admin.xaml' Line AA Position BB.

<Window.Resources>
    <model:Tag x:Key="TDD" Name="TDD" ForeColor="Black" BackColor="White" />
    <model:Tag x:Key="Agile" Name="Agile" ForeColor="White" BackColor="Black" />
    <model:Tag x:Key="Waterfail" Name="Waterfail" ForeColor="Red" BackColor="White" />
</Window.Resources>
<Window.DataContext>
    <local:AdminViewModel>
        <local:AdminViewModel.AllTags>
            <StaticResource ResourceKey="TDD"/>
            <StaticResource ResourceKey="Agile"/>
            <StaticResource ResourceKey="Waterfail"/>
        </local:AdminViewModel.AllTags>
        <local:AdminViewModel.Posts>
            <local:PostViewModel Title="Post1">
                <local:PostViewModel.Tags>
                    <StaticResource ResourceKey="TDD" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
            <local:PostViewModel Title="Post2">
                <local:PostViewModel.Tags>
                    <StaticResource ResourceKey="TDD" />
                    <StaticResource ResourceKey="Agile" />
                    <StaticResource ResourceKey="Waterfail" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
        </local:AdminViewModel.Posts>
    </local:AdminViewModel>
<Window.DataContext>

The following code compiles and runs fine, but two tags named "TDD" get created, so if I try to rename the tag, I have to do it for all of the posts, instead of just the one Tag object.

<Window.Resources>
    <model:Tag x:Key="TDD" Name="TDD" ForeColor="Black" BackColor="White" />
    <model:Tag x:Key="Agile" Name="Agile" ForeColor="White" BackColor="Black" />
    <model:Tag x:Key="Waterfail" Name="Waterfail" ForeColor="Red" BackColor="White" />
</Window.Resources>
<Window.DataContext>
    <local:AdminViewModel>
        <local:AdminViewModel.AllTags>
            <StaticResource ResourceKey="TDD"/>
            <StaticResource ResourceKey="Agile"/>
            <StaticResource ResourceKey="Waterfail"/>
        </local:AdminViewModel.AllTags>
        <local:AdminViewModel.Posts>
            <local:PostViewModel Title="Post1">
                <local:PostViewModel.Tags>
                    <model:Tag Name="TDD" ForeColor="Black" BackColor="White" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
            <local:PostViewModel Title="Post2">
                <local:PostViewModel.Tags>
                    <model:Tag Name="TDD" ForeColor="Black" BackColor="White" />
                    <model:Tag Name="Agile" ForeColor="White" BackColor="Black" />
                    <model:Tag Name="Waterfail" ForeColor="Red" BackColor="White" />
                </local:PostViewModel.Tags>
            </local:PostViewModel>
        </local:AdminViewModel.Posts>
    </local:AdminViewModel>
<Window.DataContext>

Any ideas? I'd be able to ignore it and work around it if Blend didn't render it correctly, but it does!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(2

九公里浅绿 2024-07-16 22:22:22

我可以通过在 Tags 属性中显式初始化一个新集合来避免这个问题。 像这样的事情:

<local:PostViewModel Title="Post1">
  <local:PostViewModel.Tags>
     <model:TagCollection>
        <StaticResource ResourceKey="TDD" />
     </model:TagCollection>
  </local:PostViewModel.Tags>

显然,只有当您允许 Tags 属性可写时,这才有可能。

I was able to avoid the problem by explicitly initializing a new collection within the Tags property. Something like this:

<local:PostViewModel Title="Post1">
  <local:PostViewModel.Tags>
     <model:TagCollection>
        <StaticResource ResourceKey="TDD" />
     </model:TagCollection>
  </local:PostViewModel.Tags>

Obviously, this is only possible if you allow your Tags property to be writable.

素衣风尘叹 2024-07-16 22:22:22

这与 这个问题,我想。

解决方案应该是向您的添加 x:Shared="False" 属性 元素。 这告诉 WPF 不要在每次引用实例时共享单个实例,而是创建它们的新实例。

This is very similar to the problem described in this question, I think.

The solution should be to add an x:Shared="False" attribute to your <model:Tag> elements. That tells WPF not to share a single instance each time one is referenced, and instead to create new instances of them.

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