对按需加载的程序集中资源的 Uri 引用

发布于 2024-09-08 20:02:52 字数 701 浏览 2 评论 0原文

我确实按需加载一个程序集,其中包含资源(字体)。 该程序集正在由 AssemblyPart 类加载, 因此添加到当前应用程序域。

txt1.FontFamily = New FontFamily("/SilverlightFontLibrary;component/GRAFFITO_01.ttf#Graffito")

Dim kaa = Application.GetResourceStream("/SilverlightFontLibrary;component/GRAFFITO_01.ttf".ToUri(UriKind.Relative))

字体没有应用于文本,但我确实获得了资源流。

如果程序集位于 xap 包内,则一切正常, 但将其设置为复制本地 false 它不会显示正确的字体。 :(

我无法使用 FontSource 将字体直接设置为流(我肯定有), 因为像 RunParagraphRichTextBox 这样的类根本没有它们。 ;(

有谁知道 MEF (Microsoft Extensibility Framework) 是否可以帮助我解决这个问题?

有什么已知的方法可以实现这一点吗?

我真的需要引用这些资源,但无法将它们全部放入一个 xap 包中。

:(问候

I do load an assembly on demand which holds ressources (fonts) in it.
The assembly is being loaded by the AssemblyPart class,
and therefore added to the current application domain.

txt1.FontFamily = New FontFamily("/SilverlightFontLibrary;component/GRAFFITO_01.ttf#Graffito")

Dim kaa = Application.GetResourceStream("/SilverlightFontLibrary;component/GRAFFITO_01.ttf".ToUri(UriKind.Relative))

The font is not being applied to the text, but I do get the ressource stream.

If the assembly is inside the xap package everything works fine,
but setting it to copy local false it won't show the correct font. :(

I cannot use the FontSource to set the font directly as stream (which I definately have),
because classes like Run, Paragraph or the RichTextBox simply do not have them. ;(

Does anybody know whether MEF (Microsoft Extensibility Framework) can help me out of this?

Is there any known way to accomplish that?

I seriously need to refer to those ressources, but cannot put them all into one xap package. :(

Kind regards

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

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

发布评论

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

评论(1

傾城如夢未必闌珊 2024-09-15 20:02:52

考虑解除主项目对了解字体完整 URL 的依赖。相反,在您的其他项目引用的第三个项目中创建一个 IFontProvider 接口(对 C# 表示歉意,我不使用 VB.NET):-

 public interface IFontProvider
 {
   FontFamily this[string name] {get; }
 } 

在您的字体库中创建此接口的实现:-

public class FontProvider : IFontProvider
{
  public FontFamily this[string name]
  {
     get
     {
        switch (name)
        {
            case "Graffito": 
              return New FontFamily("/SilverlightFontLibrary;component/GRAFFITO_01.ttf#Graffito");
            default:
             return null;
        }
     }   
}

使用加载到域中的库程序集您应该能够访问字体:-

Type providerType = Type.GetType("SilverlightFontLibrary.FontProvider, SilverlightFontLibrary");
IFontProvider fonts = Activator.CreateInstance(providerType) As IFontProvider;
txt1.FontFamily = fonts["Graffito"];

通过从同一组件内的代码使用“组件”url,它应该能够找到资源。

有一些关于 MEF 和动态程序集加载的博客,因此您可以使用它动态地将 IFontProvider 类型的字段与库实现连接起来。

Consider de-coupling your main project's dependency on knowing the full url to the font. Instead create an IFontProvider interface in a third project referenced by both your other projects (apologies for C# I don't do VB.NET) :-

 public interface IFontProvider
 {
   FontFamily this[string name] {get; }
 } 

In your font library create an implementation of this:-

public class FontProvider : IFontProvider
{
  public FontFamily this[string name]
  {
     get
     {
        switch (name)
        {
            case "Graffito": 
              return New FontFamily("/SilverlightFontLibrary;component/GRAFFITO_01.ttf#Graffito");
            default:
             return null;
        }
     }   
}

With the library assembly loaded into the domain you should be able to access a font:-

Type providerType = Type.GetType("SilverlightFontLibrary.FontProvider, SilverlightFontLibrary");
IFontProvider fonts = Activator.CreateInstance(providerType) As IFontProvider;
txt1.FontFamily = fonts["Graffito"];

With the "component" url being used from code within the same component it should be able to find the resource.

There are some blogs on MEF and Dynamic assembly loading so you may be able to use it dynamically wire up a field of type IFontProvider with the library implementation.

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