【发布时间】:2010-12-31 16:33:03
【问题描述】:
我有一个自定义控件,它有一个 Image 元素,其 Source 属性向用户公开,如下所示:
<ControlTemplate>
<Image x:Name="PART_Image" Source="{Binding ImageUri, RelativeSource={RelativeSource TemplatedParent}}"/>
</ControlTemplate>
其中 ImageUri 是控件类中的一个属性,如下所示:
public Uri ImageUri { get; set; }
这个自定义控件在程序集中customcontrol.dll,我可以在.exe中引用和使用这个控件没有问题,像这样:
<cc:MyControl ImageUri="/Resources/Image.png" />
其中Image.png 是 .exe 项目的资源。
但是如果我在 dll 程序集中引用和使用这个控件,就会出现问题,如果我使用像“Resources/Image.png”这样的相对uri,资源无法加载,事实证明,当这个uri应用于Image元素时,它从customcontrol.dll解析相对uri,而不是调用dll程序集,所以我想要这样做:
public Uri ImageUri {
get { ...... }
set {
if (!value.IsAbsolute) {
// Get the assembly name of parent xaml file or code
// and construct a "pack://" uri from the assembly name
// and value.OriginalString, but how ??????
}
}
}
如何获取使用我的自定义控件的 XAML 代码的程序集?
如果在代码中使用了控件,也许我可以在我的方法中使用GetCallingAssembly,但是XAML 的东西是从PresontationCore.dll 调用的,我怎样才能找到XAML 程序集???
【问题讨论】: