我定義一個變數如下:
x = Color.FromArgb(<some integer>)
并在指定矩形時按如下方式使用它:
Dim Cell As New Shapes.Rectangle With
{
.Fill = New SolidColorBrush(x),
'......
}
但是,這會給出錯誤訊息:
“顏色”型別的值無法轉換為“顏色”
這里有什么問題?
uj5u.com熱心網友回復:
有兩種不同的Color型別。
System.Drawing.Color(您正在使用的那個,GDI )System.Windows.Media.Color(WPF使用的一個)
SolidColorBrushWPF 中的A或筆刷通常期望后一種型別。
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
您可以在 WPF 中使用從Convert integer to color 中的一種方法,如下所示:
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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343694.html
