簡體   English   中英

邊框上的Silverlight動態背景(使用漸變)

[英]Silverlight Dynamic Background (using gradient) on Border

我是一位經驗豐富的.NET桌面開發人員,但是我是Silverlight的新手。 我正在嘗試將iOS應用程序轉換為Silverlight應用程序。

該應用程序基本上是使用從數據庫中提取的數據構建的項目列表。 該數據包括大量標簽文本以及前景色和背景色信息。 每個對象都是其自己的用戶控件。 它由一個內部帶有網格的Border控件(用於背景着色和圓角邊緣)組成。 我所有的標簽控件(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

所有消息框結果均符合預期,但仍然沒有背景漸變。 有人知道發生了什么嗎?

謝謝!

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

因此,顯然,通過不在此處設置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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM