相对SourceURI无法正常工作

Ala*_*ain 1 c# wpf xaml .net-4.0

我无法弄清楚适用于我的资源的相对路径.我已经尝试了我能想到的一切.唯一有效的是绝对路径只在我的系统上正确,但在任何部署版本中都是不正确的.

据我所知,这条非常简单的路径应该有效.

public class GreyscaleEffect : ShaderEffect
{
    private static PixelShader _pixelShader = new PixelShader()
        { UriSource = new Uri("/Effects/Greyscale.ps", UriKind.Relative) };
Run Code Online (Sandbox Code Playgroud)

我得到错误The type initializer for 'FSR.WPF.Utilities.UI. GreyscaleEffect' threw an exception.内部异常:Cannot locate resource 'effects/greyscale.ps'.

我也尝试过以下方法:

Uri(";component/Effects/Greyscale.ps", UriKind.Relative)
Uri("/;component/Effects/Greyscale.ps", UriKind.Relative)
Uri("/FSR.WPF.Utilities.UI;component/Effects/Greyscale.ps", UriKind.Relative)
Run Code Online (Sandbox Code Playgroud)

在其他地方(在MUSUI的xaml文件中),使用以下路径,它工作正常:

<Image Source="/FSR.WPF.Utilities.UI;component/assets/CurrencyFlags/USD.png"
Run Code Online (Sandbox Code Playgroud)

所以我无法弄清楚为什么这种类似的情况不起作用.

只有以下绝对路径有效:

Uri("C:\TFS\MUS 6.1.x\Mosaic Middleware\FSR.WPF.Utilities\FSR.WPF.Utilities.UI" +
    "\Effects\Greyscale.ps")
Run Code Online (Sandbox Code Playgroud)

我也@使用UriKind.Absolute在字符串前面使用的每个组合尝试了所有上述内容,并完全省略了第二个参数.没有什么可行,但绝对的道路,我正在失去理智.

这是解决方案结构.主项目是MUSUI,底部是粗体.此效果类和.ps文件都位于Effects文件夹中,该文件夹位于引用的程序集FSR.WPF.Utilities.UI的根目录中.

在此输入图像描述

无论如何,我知道这必须以某种方式工作.任何能够找出最短工作相对路径的人,特别是如果它不需要在程序集名称中进行硬编码,就会获得奖励.

Tho*_*que 7

您不能指定相对URI,它必须是绝对的.当您在XAML中指定相对URI时,它实际上使用转换为绝对URI IUriContext.BaseUri,但在C#代码中没有关于当前位置的信息,因此您不能使用相对URI.

您需要使用pack://URI方案指定URI:

UriSource = new Uri("pack://application:,,,/YourAssembly;component/Effects/Greyscale.ps");
Run Code Online (Sandbox Code Playgroud)