Wpf - 相对图像源路径 [英] Wpf - relative image source path

查看:89
本文介绍了Wpf - 相对图像源路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置Wpf应用程序中的图像源时遇到问题。我有一个Image,其中源绑定到DataContext对象的SourceUri属性,如下所示:

I'm having problems setting the source for images in my Wpf application. I have an Image where the source is bound to the SourceUri property of the DataContext object, like this:

<Image Source="{Binding SourceUri}"></Image>

现在,我不知道在我的对象的SourceUri属性上设置什么。设置完整的绝对路径(c:/etc/image.jpg)它显示得很好,但显然我想设置相对路径。我的图像存储在与我的应用程序文件夹位于同一文件夹中的文件夹中。最后这些图像可能来自任何地方,因此将它们添加到项目中确实不是一种选择。

Now, I don't know what to set on the SourceUri property of my object. Setting the complete absolute path ("c:/etc/image.jpg") it displays nicely, but obviously I want to set the relative path instead. My images are stored in a folder Resources which is in the same folder as my application folder. In the end these images might come from anywhere, so adding them to the project really isn't an option.

我已经尝试了相对于应用程序文件夹的路径,并且相对于工作路径(debug-folder)。还尝试使用pack:// ..语法,但没有运气,但请注意,这不是任何一点。

I've tried the path relative to the application folder, and relative to the working path (debug-folder). Also tried using the "pack://.." syntax without luck, but read that this would not be any point doing.

我应该尝试什么提示?

Any hints on what I should try?

推荐答案

也许你可以让你的DataContext对象的SourceUri属性更聪明,并确定应用程序文件夹是什么,并返回基于此的绝对路径。例如:

Perhaps you could make the SourceUri property of your DataContext object a bit cleverer, and determine what the application folder is, and return an absolute path based on that. For example:

public string SourceUri
{
    get
    {
        return Path.Combine(GetApplicationFolder(), "Resources/image.jpg");
    }
}

这篇关于Wpf - 相对图像源路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆