如何附加属性(通过 .NET 基础设施或任何其他)

发布于 2024-08-05 02:22:59 字数 1214 浏览 3 评论 0原文

我有一个 WPF 应用程序,并向项目资源添加了许多图标和位图。

现在我可以像这样访问它们:

Dim ico As System.Drawing.Icon = My.Resources.Icon 'Icon.ico
Dim img As System.Drawing.Bitmap = My.Resources.Image 'Image.png

为了在 wpf 中使用它,我创建了太简单的扩展方法,将它们转换为 ImageSource 类型:

'...Imports System.Drawing
'...Imports System.Windows.Interop.Imaging
<Extension()> _
Public Function ToImageSource(ByVal icon As Icon) As BitmapSource
    Return CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, _
        BitmapSizeOptions.FromEmptyOptions)
End Function

<Extension()> _
Public Function ToImageSource(ByVal image As Bitmap) As BitmapSource
    Return CreateBitmapSourceFromHBitmap(image.GetHbitmap(), IntPtr.Zero, _
        Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions)
End Function

所以我可以这样使用它:

Dim i As New Image With {.Source = My.Resources.Image.ToImageSource}

看一下 MyWpfExtensions.vb,我发现 Microsoft 很少有允许非官方编码的基础设施,这是我向你们的专家提出的问题。

我希望为 System.Drawing.Bitmap/Icon 类型的每个资源提供一个附加(或重写)属性,该属性通过 ex 返回图像。方法,这样我就不必在 Xaml 中使用转换器,而是直接使用它。

我实际上正在寻找类似 Microsoft.VisualBasic.MyGroupCollectionAttribute 的东西。

有什么想法吗?...

I have a WPF application and I added to the project resources many icons and bitmaps.

Now I can access them like this:

Dim ico As System.Drawing.Icon = My.Resources.Icon 'Icon.ico
Dim img As System.Drawing.Bitmap = My.Resources.Image 'Image.png

In order to use it in wpf I created too simple Extension Methods that convert them to ImageSource type:

'...Imports System.Drawing
'...Imports System.Windows.Interop.Imaging
<Extension()> _
Public Function ToImageSource(ByVal icon As Icon) As BitmapSource
    Return CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, _
        BitmapSizeOptions.FromEmptyOptions)
End Function

<Extension()> _
Public Function ToImageSource(ByVal image As Bitmap) As BitmapSource
    Return CreateBitmapSourceFromHBitmap(image.GetHbitmap(), IntPtr.Zero, _
        Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions)
End Function

So I can use it this way:

Dim i As New Image With {.Source = My.Resources.Image.ToImageSource}

Taking a look at MyWpfExtensions.vb reveals me that there are few Microsoft infrastractures that allow unofficial coding and here comes my question to the experts of you.

I'd like to have for each resource of type System.Drawing.Bitmap/Icon an additional (or overriding) property that returns the image via the ex. method so I don't have to use a converter in the Xaml, but use it directly.

I am actually looking for something like Microsoft.VisualBasic.MyGroupCollectionAttribute.

Any ideas?...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2024-08-12 02:22:59

我想除了转换器之外没有其他方法,所以让我们发布它:

Imports System.Drawing
Namespace Converters
    <ValueConversion(GetType(MarshalByRefObject), GetType(BitmapSource))> _
    Public Class ImageSourceConverter : Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, 
        parameter As Object,
        culture As System.Globalization.CultureInfo) As Object
        Implements System.Windows.Data.IValueConverter.Convert
            Dim imageSource As ImageSource = Nothing
            Dim type = value.GetType
            If type.Equals(GetType(Icon)) Then
                imageSource = DirectCast(value, Icon).ToImageSource
            ElseIf type.Equals(GetType(Bitmap)) Then
                imageSource = DirectCast(value, Bitmap).ToImageSource
            End If

            Return imageSource
        End Function

        Public Function ConvertBack(value As Object, targetType As Type,
        parameter As Object,
        culture As System.Globalization.CultureInfo) As Object Implements
        System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotSupportedException
        End Function
    End Class
End Namespace

I guess there is not other way but a converter so let's post it:

Imports System.Drawing
Namespace Converters
    <ValueConversion(GetType(MarshalByRefObject), GetType(BitmapSource))> _
    Public Class ImageSourceConverter : Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, 
        parameter As Object,
        culture As System.Globalization.CultureInfo) As Object
        Implements System.Windows.Data.IValueConverter.Convert
            Dim imageSource As ImageSource = Nothing
            Dim type = value.GetType
            If type.Equals(GetType(Icon)) Then
                imageSource = DirectCast(value, Icon).ToImageSource
            ElseIf type.Equals(GetType(Bitmap)) Then
                imageSource = DirectCast(value, Bitmap).ToImageSource
            End If

            Return imageSource
        End Function

        Public Function ConvertBack(value As Object, targetType As Type,
        parameter As Object,
        culture As System.Globalization.CultureInfo) As Object Implements
        System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotSupportedException
        End Function
    End Class
End Namespace
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文