无法指定 SolidBrushColor
Can't specify a SolidBrushColor
我定义一个变量如下:
x = Color.FromArgb(<some integer>)
并在指定矩形时按如下方式使用:
Dim Cell As New Shapes.Rectangle With
{
.Fill = New SolidColorBrush(x),
'......
}
但是,这给出了一条错误消息:
Value of type 'Color' cannot be converted to 'Color'
这里有什么问题?
有两种不同的 Color
类型。
System.Drawing.Color
(你用的那个,GDI+)
System.Windows.Media.Color
(WPF 使用的那个)
A SolidColorBrush
或 WPF 中的一般笔刷期望后者类型。
Public Sub New (color As Color)
不幸的是,此类型没有单个 int
的重载,这意味着您必须转换它并改用此 FromArgb
方法:
Public Shared Function FromArgb (a As Byte, r As Byte, g As Byte, b As Byte) As Color
您可以使用 Convert integer to color in WPF 中的一种方法,例如:
Dim bytes = BitConverter.GetBytes(/* some integer */)
Dim color = Color.FromArgb(bytes(3), bytes(2), bytes(1), bytes(0))
Dim brush = New SolidColorBrush(color)
Dim Cell As New Shapes.Rectangle With
.Fill = brush
}
我定义一个变量如下:
x = Color.FromArgb(<some integer>)
并在指定矩形时按如下方式使用:
Dim Cell As New Shapes.Rectangle With
{
.Fill = New SolidColorBrush(x),
'......
}
但是,这给出了一条错误消息:
Value of type 'Color' cannot be converted to 'Color'
这里有什么问题?
有两种不同的 Color
类型。
System.Drawing.Color
(你用的那个,GDI+)System.Windows.Media.Color
(WPF 使用的那个)
A SolidColorBrush
或 WPF 中的一般笔刷期望后者类型。
Public Sub New (color As Color)
不幸的是,此类型没有单个 int
的重载,这意味着您必须转换它并改用此 FromArgb
方法:
Public Shared Function FromArgb (a As Byte, r As Byte, g As Byte, b As Byte) As Color
您可以使用 Convert integer to color in WPF 中的一种方法,例如:
Dim bytes = BitConverter.GetBytes(/* some integer */)
Dim color = Color.FromArgb(bytes(3), bytes(2), bytes(1), bytes(0))
Dim brush = New SolidColorBrush(color)
Dim Cell As New Shapes.Rectangle With
.Fill = brush
}