代码之家  ›  专栏  ›  技术社区  ›  tomjen

这个URI有什么问题?

  •  6
  • tomjen  · 技术社区  · 16 年前

    海吉

    我正试图使用一个URI在WPF应用程序中加载(嵌入的)映像,但我一直收到一个异常。

    代码是:

    new BitmapImage(new Uri("pack://application:,,,,/Icons/m.png"));
    

    (如果不清楚,我尝试从icons文件夹加载m.png文件,该文件夹 已标记为嵌入资源)。

    例外是

    NotSupportetException (the URI prefix is not recognized)
    

    有人能告诉我URI应该是什么吗?

    2 回复  |  直到 16 年前
        1
  •  2
  •   Darin Dimitrov    16 年前

    你可以看看 this blog post . 解决方案是注册一个自定义的URI解析器,以便它识别 pack 协议:

    UriParser.Register(
        new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1
    );
    
        2
  •  4
  •   Eugene Cheverda    16 年前

    字符串中必须有三个逗号而不是四个逗号:

    new BitmapImage(new Uri("pack://application:,,,/LibName;component/Icons/m.png"));
    

    libname-指向承载资源的程序集。