【问题标题】:How to use a localizable resx image in WPF?如何在 WPF 中使用可本地化的 resx 图像?
【发布时间】:2012-09-24 05:31:11
【问题描述】:

我在标准Properties.Resources.resx 文件中有一堆可本地化的图像和图标;它们大多在编译时链接。我想把它们放在一些 WPF 控件上。

在 WinForms 中,

control.Image = Properties.Resources.ImageResourceName;

有效,可维护,可本地化,不易重构,并且可以仅使用 Windows 窗体设计器完成,但我只是不知道如何在 WPF 中实现类似的结果(xaml 或代码)。

【问题讨论】:

    标签: wpf embedded-resource resx


    【解决方案1】:

    您的 resx 文件中存储的是 Drawing.Bitmap。 您可以使用下面的代码将其转换为 BitmapSource。 例如,此代码可以放置在转换器中。 个人而言,我定义了一个 MarkupExtension,它采用 resx 的名称和 ressource 的名称并调用此代码。

    private BitmapSource bitmapToSource(System.Drawing.Bitmap bitmap)
    {
        BitmapSource destination;
        IntPtr hBitmap = bitmap.GetHbitmap();
        BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
        destination = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
        destination.Freeze();
        return destination;
    }
    

    【讨论】:

    • 在本应用户友好的 WPF 中,像可本地化图像这样实用且简单的东西怎么可能变得如此不必要地复杂?
    • 嗯,本地化和图形已经过重新考虑。似乎它们已经被独立地重新考虑......并且不再那么容易一起使用。但是,即使不是内置解决方案,您也必须编写一次,再次集成本地化图像变得容易
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多