1

我正在开发适用于 Windows 8 的 Metro UI 应用程序,并且正在尝试从项目文件夹中检索图像。

这是我的初始代码:

public static Image GetImage(string path, int width, int height, int margin)
    {
        Image img = new Image();
        img.Width = width;
        img.Height = height;
        img.Margin = new Windows.UI.Xaml.Thickness(margin);

        BitmapImage bi = new BitmapImage(new Uri(path, UriKind.Absolute));

        img.Source = bi;

        return img;
    }

和方法调用:

Image = Extension.GetImage("Classe;component/Images/faq.png", 100, 100, 0);

和错误:

Exception:Thrown: "Invalid URI: The format of the URI could not be determined." (System.UriFormatException)
A System.UriFormatException was thrown: "Invalid URI: The format of the URI could not be determined."

如果我将 URI 类型更改为相对:

Exception:Thrown: "The given System.Uri cannot be converted into a Windows.Foundation.Uri. Please see http://go.microsoft.com/fwlink/?LinkID=215849 for details." (System.ArgumentException)
A System.ArgumentException was thrown: "The given System.Uri cannot be converted into a Windows.Foundation.Uri. Please see http://go.microsoft.com/fwlink/?LinkID=215849 for details."

图片路径是图片/faq.png。设置为内容并始终复制。

4

1 回答 1

0

查看 Windows.Foundation.Uri 的文档,看起来 WinRT 不支持相对 URI。MS 为 WinRT 资源发明了另一种 URI 格式。

尝试这样的事情,看看它是否有效:

new Uri("Classe://MyAssembly/component/Images/faq.png") 

注意:您不要添加您的实际程序集名称 - MyAssembly 部分是 LITERAL TEXT。

于 2012-08-10T16:16:07.763 回答