使用地铁和图像时引发的 URI 异常



我正在开发适用于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."
图像

路径是图像/常见问题解答.png。 设置为内容并始终复制。

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

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

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

注意:您不会添加实际的程序集名称 - MyAssembly 部件是文字文本。

最新更新