边框上的Silverlight动态背景(使用渐变)

时间:2012-01-01 18:56:15

标签: silverlight background border lineargradientbrush

我是一位经验丰富的.NET桌面开发人员,但我是Silverlight的新手。我正在尝试将iOS应用程序转换为Silverlight应用程序。

该应用程序基本上是使用从数据库中提取的数据构建的项目列表。该数据包括大量标签文本以及前景色和背景色信息。每个对象都是自己的用户控件。它由一个边框控件(用于背景着色和圆角边缘)和一个Grid内部组成。我的所有标签控件(TextBlocks)都在Grid中。

这些颜色值(前景和背景)中的每一个都以逗号分隔的字符串形式出现在数据库中(即“{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 个答案:

答案 0 :(得分: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