如何从里面XAML .resx文件引用的图标? [英] How to reference icons inside .resx files from xaml?

查看:283
本文介绍了如何从里面XAML .resx文件引用的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个C#WPF应用程序,使用.resx文件资源管理。现在,我想图标(ICO)添加到项目,但我遇到了一些问题。

I am working on a C# WPF application, using .resx files for resource management. Now, I'm trying to add icons (.ico) to the project but I'm running into some problems.

<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>

这工作得很好,但是当我移动图标进入鸭presources.resx我碰到问题在XAML code引用它。我应该怎么使用,而不是setter属性= ...线上面?这:

This works fine, but when I move the icon into AppResources.resx I run into problems with referencing it in the xaml code. What should I be using instead of the Setter Property=... lines above? This:

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

不工作,我想我可能需要使用不同的属性不是源,因为不是值的字符串指向图标,但现在该图标本身。我想不出来,虽然使用哪一个? - 一些帮助,请

doesn't work, I think I probably need to use a different Property than "Source" because Value isn't a string pointing to the icon but the icon itself now. I can't figure out which one to use though - some help, please?

推荐答案

来源属性不想的字符串,它只是将其转换时,它得到的。如果添加一个图标资源,这将是类型 System.Drawing.Icon 的。您需要将它通过转换器转换为的ImageSource

The Source property does not "want" a string, it just converts it when it gets one. If you add an icon to the resources it will be of the type System.Drawing.Icon. You will need to convert it to an ImageSource via converter.

您可以做一个静态的对资源的访问,但它需要遵守的 X:静态

You can do a static access to resources but it needs to comply with the expected syntax of 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();
    }
}

注:

  • 在资源访问修饰符必须是public
  • 如果该图像将作为图像你结束了一个位图,而不是一个图标,这就需要不同的转换器
  • The resource access modifier must be public
  • If the image is added as "Image" you end up with a Bitmap instead of an Icon, which requires a different converter

这篇关于如何从里面XAML .resx文件引用的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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