【问题标题】:How to reference icons inside .resx files from xaml?如何从 xaml 引用 .resx 文件中的图标?
【发布时间】:2011-04-26 12:51:40
【问题描述】:

我正在开发一个 C# WPF 应用程序,使用 .resx 文件进行资源管理。现在,我正在尝试将图标 (.ico) 添加到项目中,但遇到了一些问题。

<Image Name="imgMin" Grid.Column="0"
       Stretch="UniformToFill"
       Cursor="Hand" 
       MouseDown="imgMin_MouseDown">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="\Images\minimize_glow.ico"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Source" Value="\Images\minimize_glow.ico"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

这很好用,但是当我将图标移动到 AppResources.resx 时,我遇到了在 xaml 代码中引用它的问题。我应该使用什么来代替上面的 Setter Property=... 行?这个:

<Setter Property="Source" Value="{x:Static res:AppResources.minimize}"/>

不起作用,我想我可能需要使用与“Source”不同的属性,因为 Value 现在不是指向图标的字符串,而是指向图标本身。我不知道该使用哪一个 - 请帮忙?

【问题讨论】:

    标签: c# .net wpf data-binding icons


    【解决方案1】:

    Source 属性并不“想要”一个字符串,它只是在得到一个字符串时对其进行转换。如果您将图标添加到资源,它将属于System.Drawing.Icon 类型。您需要通过转换器将其转换为ImageSource

    您可以对资源进行静态访问,但它需要符合x:Static 的预期语法。

    例如

    xmlns:prop="clr-namespace:Test.Properties"
    
    <Image MaxHeight="100" MaxWidth="100">
        <Image.Source>
            <Binding Source="{x:Static prop:Resources.icon}">
                <Binding.Converter>
                    <vc:IconToImageSourceConverter/>
                </Binding.Converter>
            </Binding>
        </Image.Source>
    </Image>
    
    public class IconToImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var icon = value as System.Drawing.Icon;
            var bitmap = icon.ToBitmap();
    
            //http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap/1069509#1069509
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ms.Position = 0;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
    
            return bi;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    注意事项:

    • 资源访问修饰符必须是公共的
    • 如果图像被添加为“图像”,您最终会得到 Bitmap 而不是图标,这需要不同的转换器

    【讨论】:

    • 非常有用...我认为。我现在收到一条错误消息:未知构建错误,'密钥不能为空。参数名称:key Line 131 Position 34.'指向绑定源="{x:Static res:AppResources.minimize}"
    • 重要的一点是资源访问修饰符是公共的而不是内部的,但这会引发另一个异常。
    • 出于向后兼容性的原因,我使用的是 .NET 3.5 而不是 4.0 - 我不认为这可能是个问题?
    • 不,我只是继续在一个新的 .NET 3.5 项目中测试它,它工作正常。
    • 嗯,没想到会是这样。但是,我现在也遇到了最奇怪的错误:找不到类型“Helpers:IconToImageSourceConverter”。确认您没有丢失程序集引用并且所有引用的程序集都已构建。 我在代码隐藏中找到了正确的 using 行,xmlns:Helpers 指向正确的命名空间...什么鬼?
    猜你喜欢
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2012-02-04
    相关资源
    最近更新 更多