2012-01-01 19 views
1

我是一位經驗豐富的.NET桌面開發人員,但我是Silverlight的新手。我試圖將iOS應用程序轉換爲Silverlight應用程序。邊框上的Silverlight動態背景(使用漸變)

該應用程序基本上是使用從數據庫中提取的數據構建的項目列表。這些數據包含大量標籤文本以及前景和背景顏色信息。每個對象都是它自己的用戶控件。它由一個Border控件(用於背景着色和圓角邊緣)和一個Grid內部組成。我所有的標籤控件(TextBlocks)都在網格中。

這些顏色值(前景和背景)中的每一個都以逗號分隔的字符串(即「{r},{g},{b}」)出現在數據庫之外。

因此,我將這些值轉換爲代碼中的實際顏色對象。然後,我將我的標籤的前景屬性設置爲這種顏色。

所有這些(標籤文本分配和前景色)工作得很好。不起作用的是將背景顏色轉換爲線性漸變畫筆。我目前使用數據庫中的顏色作爲「基礎」顏色,並使用此顏色計算4色漸變。 (數字並不重要,但我將RGB值調整爲基本顏色的1.4,1.2,0.8和0.6)。

下面是創建自定義的線性漸變畫筆代碼:

Friend Function CalculateColorsFromBaseColor(ByVal baseColor As Color) As List(Of Color) 
    Dim retList As New List(Of Color) 
    Dim r As Byte = baseColor.R 
    Dim g As Byte = baseColor.G 
    Dim b As Byte = baseColor.B 
    retList.Add(New Color With {.R = If(r * 1.4 > 255, 255, r * 1.4), .G = If(g * 1.4 > 255, 255, g * 1.4), .B = If(b * 1.4 > 255, 255, b * 1.4)}) 
    retList.Add(New Color With {.R = If(r * 1.2 > 255, 255, r * 1.2), .G = If(g * 1.2 > 255, 255, g * 1.2), .B = If(b * 1.2 > 255, 255, b * 1.2)}) 
    retList.Add(New Color With {.R = If(r * 0.8 > 255, 255, r * 0.8), .G = If(g * 0.8 > 255, 255, g * 0.8), .B = If(b * 0.8 > 255, 255, b * 0.8)}) 
    retList.Add(New Color With {.R = If(r * 0.6 > 255, 255, r * 0.6), .G = If(g * 0.6 > 255, 255, g * 0.6), .B = If(b * 0.6 > 255, 255, b * 0.6)}) 
    Return retList 
End Function 

Friend Function CalculateLinearGradientBrushFromBaseColor(ByVal baseColor As Color) As LinearGradientBrush 
    Dim lgb As New LinearGradientBrush With {.StartPoint = New Point(0.5, 0), .EndPoint = New Point(0.5, 1)} 
    Dim colors As List(Of Color) = CalculateColorsFromBaseColor(baseColor) 
    lgb.GradientStops.Add(New GradientStop With {.Color = colors.Item(0), .Offset = 0.0}) 
    lgb.GradientStops.Add(New GradientStop With {.Color = colors.Item(1), .Offset = 0.5}) 
    lgb.GradientStops.Add(New GradientStop With {.Color = colors.Item(2), .Offset = 0.5}) 
    lgb.GradientStops.Add(New GradientStop With {.Color = colors.Item(3), .Offset = 1.0}) 
    Return lgb 
End Function 

這裏有一個如何我想在運行時將這一代碼:

Dim backColorString As String = iCase.CaseColor 
Dim backColorRGB As String() = backColorString.Split(",") 
Dim backColor As Color = Color.FromArgb(255, CInt(backColorRGB(0)), CInt(backColorRGB(1)), CInt(backColorRGB(2))) 
caseCell.BackgroundBorder.Background = caseCell.CalculateLinearGradientBrushFromBaseColor(backColor) 

當我設置背景漸變在設計時手動在XAML中,它看起來是正確的。當我嘗試從代碼隱藏的角度來看時,看起來我根本沒有背景。 (當整個頁面的背景是白色時,我的用戶控件的顏色也是如此,當黑色時,它是黑色的,因此,看起來用戶控件的背景變得透明)。

試圖調試這個,已經添加以下代碼在我的後臺任務:

'' Trying to see what the background values are prior to setting it 
For Each iStop As GradientStop In CType(caseCell.BackgroundBorder.Background, LinearGradientBrush).GradientStops 
    MessageBox.Show(String.Format("iStop Color: ({0},{1},{2})", iStop.Color.R, iStop.Color.G, iStop.Color.B)) 
Next 
'' Setting the background 
caseCell.BackgroundBorder.Background = caseCell.CalculateLinearGradientBrushFromBaseColor(backColor) 
'' Checking the values after setting 
For Each iStop As GradientStop In CType(caseCell.BackgroundBorder.Background, LinearGradientBrush).GradientStops 
    MessageBox.Show(String.Format("iStop Color: ({0},{1},{2})", iStop.Color.R, iStop.Color.G, iStop.Color.B)) 
Next 

所有消息框結果都如預期,不過,我沒有得到任何背景漸變。有人知道發生了什麼事嗎?

謝謝!

回答

1

愚蠢,愚蠢,愚蠢!!!!!

所以,很顯然,通過這裏沒有設置alpha值:

Friend Function CalculateColorsFromBaseColor(ByVal baseColor As Color) As List(Of Color) 
    Dim retList As New List(Of Color) 
    Dim r As Byte = baseColor.R 
    Dim g As Byte = baseColor.G 
    Dim b As Byte = baseColor.B 
    retList.Add(New Color With {.R = If(r * 1.4 > 255, 255, r * 1.4), .G = If(g * 1.4 > 255, 255, g * 1.4), .B = If(b * 1.4 > 255, 255, b * 1.4)}) 
    retList.Add(New Color With {.R = If(r * 1.2 > 255, 255, r * 1.2), .G = If(g * 1.2 > 255, 255, g * 1.2), .B = If(b * 1.2 > 255, 255, b * 1.2)}) 
    retList.Add(New Color With {.R = If(r * 0.8 > 255, 255, r * 0.8), .G = If(g * 0.8 > 255, 255, g * 0.8), .B = If(b * 0.8 > 255, 255, b * 0.8)}) 
    retList.Add(New Color With {.R = If(r * 0.6 > 255, 255, r * 0.6), .G = If(g * 0.6 > 255, 255, g * 0.6), .B = If(b * 0.6 > 255, 255, b * 0.6)}) 
    Return retList 
End Function 

我正在透明的顏色。

愚蠢,愚蠢,愚蠢!以下是工作代碼:

Friend Function CalculateColorsFromBaseColor(ByVal baseColor As Color) As List(Of Color) 
    Dim retList As New List(Of Color) 
    Dim r As Byte = baseColor.R 
    Dim g As Byte = baseColor.G 
    Dim b As Byte = baseColor.B 
    retList.Add(New Color With {.A = 255, .R = If(r * 1.4 > 255, 255, r * 1.4), .G = If(g * 1.4 > 255, 255, g * 1.4), .B = If(b * 1.4 > 255, 255, b * 1.4)}) 
    retList.Add(New Color With {.A = 255, .R = If(r * 1.2 > 255, 255, r * 1.2), .G = If(g * 1.2 > 255, 255, g * 1.2), .B = If(b * 1.2 > 255, 255, b * 1.2)}) 
    retList.Add(New Color With {.A = 255, .R = If(r * 0.8 > 255, 255, r * 0.8), .G = If(g * 0.8 > 255, 255, g * 0.8), .B = If(b * 0.8 > 255, 255, b * 0.8)}) 
    retList.Add(New Color With {.A = 255, .R = If(r * 0.6 > 255, 255, r * 0.6), .G = If(g * 0.6 > 255, 255, g * 0.6), .B = If(b * 0.6 > 255, 255, b * 0.6)}) 
    Return retList 
End Function