【问题标题】:WPF Resource Dictionary XSLTWPF 资源字典 XSLT
【发布时间】:2010-05-19 02:59:24
【问题描述】:

要在 wpf 中使用图像,您可以定义:

<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <BitmapImage x:Key="ImageFunTime"
                 UriSource="../Resources/Images/ImageFunTime.png" />
</

然后在应用程序的某个地方你可以:

var img = (ImageSource)positionsTab.TryFindResource("ImageFunTime");

如何使用嵌入式 Xslt 文件实现相同的功能?也就是说,资源字典中的语法是什么,因为它显然不是位图图像......

TIA

【问题讨论】:

  • 好问题 (+1)。请参阅我的答案以获得完整的解决方案。 :)

标签: c# wpf resources resourcedictionary


【解决方案1】:

我怎样才能达到同样的目的 嵌入的 Xslt 文件?

答案

  1. Microsoft 的 XSLT 处理器都不支持嵌入式 XSLT 样式表。您需要为您的 XSLT 样式表提供一个完整的、仅限 XSLT 的文件。

  2. 在 XSLT 中,使用&lt;xsl:key&gt; 指令和key() 函数通过某个字符串值有效地选择节点,从而唯一地标识它们。

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="UriSourceByKey"
  match="@UriSource" use="../@x:Key"/>

 <xsl:variable name="vImageFunTime"
  select="key('UriSourceByKey', 'ImageFunTime')"/>

 <xsl:template match="/">
  <xsl:value-of select="$vImageFunTime"/>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时

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

  <BitmapImage x:Key="ImageFunTime"  UriSource="../Resources/Images/ImageFunTime.png" />
</ResourceDictionary>

产生想要的结果

../Resources/Images/ImageFunTime.png

【讨论】:

  • 我认为 OP 想要将 XSLT 样式表声明为资源(如相关图像),然后将其检索到 C# 应用程序中。
  • @Alejandro:那么,如果他问资源的类型应该是什么,答案是:字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多