如何获取自定义控件中引用 XAML 的基本 uri

发布于 2024-08-17 02:10:34 字数 1254 浏览 3 评论 0原文

我有一个自定义控件,它有一个 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 程序集中引用并使用此控件,则会出现问题,如果我使用像这样的相对 uri,我必须使用绝对“pack://...” uri 来引用调用 dll 中的图像“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,但是XAML内容是从PresontationCore.dll调用的,我怎样才能找到XAML程序集? ?

I have an custom control, which has an Image element with its Source property exposed to user, like this:

<ControlTemplate>
    <Image x:Name="PART_Image" Source="{Binding ImageUri, RelativeSource={RelativeSource TemplatedParent}}"/>
</ControlTemplate>

where ImageUri is an property in the control class, like this:

public Uri ImageUri { get; set; }

This custom control is in an assembly customcontrol.dll, I can reference and use this control in an .exe with no problem, like this:

<cc:MyControl ImageUri="/Resources/Image.png" />

where Image.png is a resource of the .exe project.

But if I reference and use this control in an dll assembly, there is a problem, I have to use a absolute "pack://..." uri to reference an image in the calling dll, if I use an relative uri like "Resources/Image.png", the resource cant be loaded, it turns out, when this uri is applied on the Image element, it resolves the relative uri from customcontrol.dll, not the calling dll assembly, so I want to do this:

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 ??????
        }
    }
}

How can I get the assembly of the XAML code that uses my custom control?

If the control is used in code maybe I can use GetCallingAssembly in my methods, but XAML stuff is called from PresontationCore.dll, how can I find out the XAML assembly???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-08-24 02:10:34

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

OK I have found a solution myself. I should implement IUriContext interface,
which has only one property: Uri BaseUri, this is exactly what I want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文