使用X时出错:假QUOT共享= QUOT;在外部装配在WPF资源 [英] Error when using x:Shared="False" resources in external assembly in WPF

查看:228
本文介绍了使用X时出错:假QUOT共享= QUOT;在外部装配在WPF资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们有一个 ResourceDictionary 和一些图标,如下所示:

In our project we have a ResourceDictionary with some Icons that looks like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Canvas x:Key="Icon.Refresh"
            x:Shared="False"
            Width="32"
            Height="32"
            Clip="F1 M 0,0L 32,0L 32,32L 0,32L 0,0">
        <Path .../>
        <Path .../>
        <Path .../>
    </Canvas>
</ResourceDictionary>

x:Shared =False属性,因为否则当我在多个视图中使用它时,图标将消失。

The x:Shared="False" attribute is needed, because else the icon would disappear when I use it in multiple views.

现在,我们想使用相同的图标创建另一个项目,

Now we want to make another project with the same icons, so we decided to put them in a library project that is referenced by both projects.

但是当我们尝试运行应用程序时,我们总是得到错误:

But when we try to run the application we always get the error:


命名空间中的共享属性 http:// schemas .microsoft.com / winfx / 2006 / xaml 只能在已编译的资源字典中使用。

Shared attribute in namespace "http://schemas.microsoft.com/winfx/2006/xaml" can be used only in compiled resource dictionaries."

不能摆脱 x:Shared =False属性,因为据我所知,它是阻止图标消失的唯一方法。

but we can't get rid of the x:Shared="False" attribute, because as far as I know it's the only way to stop the icons from disappearing.

因此,我们可以做什么,通过项目引用共享多个项目的图标,而不会消失图标?

So we what can we do, to share the icons over multiple projects with a project reference and without disappearing icons?

推荐答案

通过这个错误判断,我们可以理解 x:Shared 属性只能用于编译的 ResourceDictionary 。引自 MSDN x:共享属性

Judging by this error, we can understand that x:Shared attribute can be used only for compiled ResourceDictionary. Quote from MSDN x:Shared Attribute:


包含具有 x:Shared 的项目的ResourceDictionary 必须被编译 。 ResourceDictionary不能在松散的XAML内,也不能用于主题。

The ResourceDictionary that contains the items with x:Shared must be compiled. The ResourceDictionary cannot be within loose XAML or used for themes.

编译 ResourceDictionary 构建操作以设置 ,因为在这种情况下,到BAML(二进制应用程序标记语言)。此属性通常在创建新 ResourceDictionary 时默认设置。

Compiled ResourceDictionary is one that Build action to set Page, as in this case, it is converted to BAML (Binary Application Markup Language) at run-time. This attribute usually be set by default when creating new ResourceDictionary.

BAML 只是XAML,已将
解析,标记并转换为二进制形式使用XAML文件的性能。引用Adam Nathan WPF的书:

BAML is simply XAML that has been parsed, tokenized, and converted into binary form to increase performance for working with XAML files. Quote from Adam Nathan WPF book:


BAML不像Microsoft中间体
语言(MSIL)。它是一种压缩的声明性格式,比普通的XAML加载和解析(和
的大小更小)。 BAML基本上是
XAML编译过程的实现细节。

BAML is not like Microsoft intermediate language (MSIL); it is a compressed declarative format that is faster to load and parse (and smaller in size) than plain XAML. BAML is basically an implementation detail of the XAML compilation process.

因此,建议您在 ResourceDictionary 中检查此标记,它会被设置为 Resource ,在内存中将被存储为未打包的XAML版本,这可能会影响整个WPF应用程序的性能。

Therefore it is always advisable to check this flag in ResourceDictionary, because if it will be set Resource, in the memory will be stored not packaged version of XAML, which later may affect to the performance of the whole WPF application.

这篇关于使用X时出错:假QUOT共享= QUOT;在外部装配在WPF资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆