给出Null引用异常的GetManifestResourceStream(参数名称:streamSource)

时间:2012-02-17 12:14:35

标签: c# visual-studio-2010 visual-studio-lightswitch

我继续从GetManifestResourceStream获取一个null引用异常,我试图将Logo图像添加到Lightswitch功能区,它应该可以正常工作....

指的是LR__ http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/2d16c638-f833-4c4c-beec-656912a87b8e/#76fa5382-0135-41ba-967c-02efc3f8c3a2

System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
image.SetSource(Assembly.GetExecutingAssembly().GetManifestResourceStream(
    Application.Current.Details.Name + ".Resources.logo.jpg"));
Image myImage = new Image()
{
    Source = image,
    Stretch = System.Windows.Media.Stretch.Uniform,
    HorizontalAlignment = HorizontalAlignment.Left,
    Margin = new Thickness(2, 2, 2, 14),
    Cursor = System.Windows.Input.Cursors.Hand
};

我尝试了很多东西,但我找不到问题所在的地方!!

4 个答案:

答案 0 :(得分:4)

在项目中添加您的图像或文件,然后在解决方案资源管理器窗口中选择您的文件,然后在属性窗口中选择Build Action,然后设置值" Embedded Resource"构建动作属性

就是这个

enter image description here

答案 1 :(得分:3)

Logo.jpg是否将其构建操作设置为“嵌入式资源”?

编辑:

这里是我的GetResourceUri的C#转换(注意,它需要资源,而不是嵌入式资源):

public Uri GetResourceUri(this Assembly asm, string resourceName)
{
    Uri result = null;
    var assemblyName = new AssemblyName(asm.FullName).Name;

    result = new Uri(string.Format("/{0};component/{1}", assemblyName, resourceName), UriKind.Relative);

    return result;
}

相同的“技术”在C#中工作。

我还有一个自定义shell扩展(使用LR的技术将图像添加到功能区和导航菜单中)。我只是完成了一些事情(写“文档”需要一些时间)&然后我将在Visual Studio Gallery上发布它以供社区使用(它被称为Luminous Classic Shell)。

扩展程序允许您拥有图像而无需编写代码。

答案 2 :(得分:0)

您可以使用Reflector等工具查看Assembly中资源的全名。

答案 3 :(得分:0)

我有这个null返回问题&我正在撕掉我的头发因为使用图像文件作为嵌入式资源是我的标准技巧之一。

我最终发现原因是我从一位使用Apple& amp;的平面设计师那里收到了文件。他们没有工作。我摆弄着权限;使用Paint程序以不同的格式保存它们但没有任何效果。 最后我在Paint中创建了一个全新的文件,然后复制了&粘贴原始图像中的像素。然后它工作了。

只是为了记录,有谁知道为什么会这样?它必须以某种方式位于标题块中。