【问题标题】:Wpf - relative image source pathWpf - 相对图像源路径
【发布时间】:2010-11-29 12:06:13
【问题描述】:

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

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

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

我尝试了相对于应用程序文件夹的路径,以及相对于工作路径(调试文件夹)的路径。还尝试使用“pack://..”语法但没有运气,但读到这样做没有任何意义。

关于我应该尝试什么的任何提示?

【问题讨论】:

    标签: wpf image path


    【解决方案1】:

    System.IO.Path 中有一个方便的方法可以帮助解决这个问题:

    return Path.GetFullPath("Resources/image.jpg");
    

    这应该返回 'C:\Folders\MoreFolders\Resources\image.jpg' 或您上下文中的任何完整路径。它将使用当前工作文件夹作为起点。

    Link to MSDN documentation on GetFullPath.

    【讨论】:

    • 这很棒 - 如果我想提供自己的完整路径,它仍然有效。
    【解决方案2】:

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

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

    【讨论】:

    • 当然——行得通——谢谢!虽然找不到找到应用程序文件夹的好方法,所以现在感觉有点hacky..Net中有GetApplicationFolder()吗?找不到一个..但是相对路径引用不应该以某种方式工作吗?然后这些将相对于应用程序根文件夹?没有?
    • 尝试 Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    • 这给了我与我正在使用的相同,“Path.GetFullPath("."),但我猜后者取决于工作文件夹。所以按照你的建议。我得到了还有一些关于这个的问题,但我会弄清楚的。谢谢你的关注!
    【解决方案3】:

    经过一些令人沮丧的尝试

     <Image Source="pack://application:,,,/{Binding ChannelInfo/ChannelImage}">
    

     <Image Source="pack://siteoforigin:,,,/{Binding ChannelInfo/ChannelImage}">
    

     <Image Source="/{Binding ChannelInfo/ChannelImage}">
    

    我通过实现自己的转换器解决了这个问题:

    C#端:

    public class MyImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string path= (string)value;
    
            try
            {
                //ABSOLUTE
                if (path.Length > 0 && path[0] == System.IO.Path.DirectorySeparatorChar
                    || path.Length > 1 && path[1] == System.IO.Path.VolumeSeparatorChar)
                    return new BitmapImage(new Uri(path));
    
                //RELATIVE
                return new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + path));
            }
            catch (Exception)
            {
                return new BitmapImage();
            }
    
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    XAML 端:

    <UserControl.Resources>
        <local:ImageConverter x:Key="MyImageConverter" />
        (...)
    </UserControl.Resources>
    
    <Image Source="{Binding Products/Image, Converter={StaticResource MyImageConverter}}">
    

    干杯,

    塞尔吉奥

    【讨论】:

      【解决方案4】:

      Environment.CurrentDirectory 将向您显示 .exe 所在的文件夹(除非您手动设置 .CurrentDirectory - 但我们可以假设您已经知道它的位置)。

      【讨论】:

        猜你喜欢
        • 2011-08-25
        • 2013-07-25
        • 1970-01-01
        • 2012-01-10
        • 2018-11-05
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        • 2012-04-30
        相关资源
        最近更新 更多