【问题标题】:How to get base uri of referencing XAML in a custom control如何在自定义控件中获取引用 XAML 的基本 uri
【发布时间】: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 程序集???

【问题讨论】:

    标签: wpf xaml uri


    【解决方案1】:

    好的,我自己找到了解决方案。我应该实现 IUriContext 接口, 它只有一个属性:Uri BaseUri,这正是我想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2018-12-21
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多