Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何引用随my metro风格应用程序打包的图像源文件?_C#_Windows 8_Microsoft Metro - Fatal编程技术网

C# 如何引用随my metro风格应用程序打包的图像源文件?

C# 如何引用随my metro风格应用程序打包的图像源文件?,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,我有一个.png文件作为我的应用程序的内容。当我像这样用xaml绑定它时 <ImageBrush x:Key="BtnBackImageBrush" ImageSource="/Assets/Images/back.png" /> 我的问题是如何引用我的metro风格应用程序打包的图像源文件 谢谢你的建议 更新: 我找到了答案! 我们需要使用父FrameworkElement设置baseUri,而不是手动设置。例如: // Usage myImage.Source = ImageF

我有一个.png文件作为我的应用程序的内容。当我像这样用xaml绑定它时

<ImageBrush x:Key="BtnBackImageBrush" ImageSource="/Assets/Images/back.png" />
我的问题是如何引用我的metro风格应用程序打包的图像源文件

谢谢你的建议

更新: 我找到了答案! 我们需要使用父FrameworkElement设置baseUri,而不是手动设置。例如:

// Usage
myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content");

public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
{
    var uri = new Uri(parent.BaseUri, path);
    BitmapImage result = new BitmapImage();
    result.UriSource = uri;
    return result;
}

谢谢。是的,你是对的,这就是你问题的答案

img.Source = ImageFromRelativePath(this, "Assets/Images/back.png");

public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
{
    var uri = new Uri(parent.BaseUri, path);
    BitmapImage bmp = new BitmapImage();
    bmp.UriSource = uri;
    return bmp;
}
新Uri(“ms:appx”)

我认为这就是问题的根源。该方案是ms appx而不是ms:appx

错误的URI:
ms:appx://Assets/Images/back.png

好的URI:
ms-appx://Assets/Images/back.png


但是使用FrameworkElement并不是一个坏主意,如果您真的试图定义类似于其父元素的内容,那么即使两者都起作用,后者对读者来说可能更清楚您的意图(假设这就是您的意图)。

应该是ms appx:///注意3/
img.Source = ImageFromRelativePath(this, "Assets/Images/back.png");

public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
{
    var uri = new Uri(parent.BaseUri, path);
    BitmapImage bmp = new BitmapImage();
    bmp.UriSource = uri;
    return bmp;
}