我有一个自定义控件,它有一个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程序集中引用并使用此控件,则存在一个问题,我必须使用绝对的“pack:// ...”uri来引用调用dll中的图像,如果我使用的话相对uri喜欢“Resources / Image.png”,资源无法加载,事实证明,当这个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
,但是从PresontationCore.dll
调用XAML的东西,我怎样才能找到XAML程序集???
答案 0 :(得分:1)
好的,我自己找到了解决方案。我应该实现IUriContext接口, 它只有一个属性:Uri BaseUri,这正是我想要的。