边框上的 Silverlight 动态背景(使用渐变)
我是一位相当有经验的 .NET 桌面开发人员,但我对 Silverlight 还很陌生。我正在尝试将 iOS 应用程序转换为 Silverlight 应用程序。
该应用程序基本上是使用从数据库中提取的数据构建的项目列表。该数据包括大量标签文本以及前景色和背景色信息。每个对象都是其自己的用户控件。它由一个边框控件(用于背景色和圆角边缘)和一个内部网格组成。我的所有标签控件(TextBlock)都位于网格内部。
这些颜色值(前景和背景)中的每一个都以逗号分隔的字符串(即“{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
所有消息框结果都符合预期,但我仍然没有得到背景渐变。有人知道发生了什么事吗?
谢谢!
I'm a fairly experienced .NET desktop developer, but I'm new to Silverlight. I'm trying to convert an iOS application to a Silverlight app.
The app is basically a list of items that are built using data pulled from a database. This data includes a large number of label texts as well as foreground and background color information. Each object is its own user control. It consists of a Border control (for background coloring and rounded edges) with a Grid inside. All of my label controls (TextBlocks) are inside of the Grid.
Each of these color values (foreground and background) come out of the database as a comma-delimited string (i.e., "{r},{g},{b}").
So, I convert those values to actual color objects in code. Then, I set the foreground property of my labels to this color.
All of this (label text assignments and foreground color) is working very well. What's NOT working is converting the background color into a linear gradient brush. I'm currently using the color from the database as a "base" color and calculating a 4-color gradient from this color. (The numbers aren't important, but I adjust the RGB values to 1.4, 1.2, 0.8 and 0.6 of the base color).
Here's the code to create the custom linear gradient brush:
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
Here's the code for how I'm trying to incorporate this at runtime:
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)
When I set a background gradient manually in XAML at design-time, it appears correctly. When I try to do it from the code-behind, it appears that I get no background at all. (When the overall page's background is white, so is the color of my user control. When black, it's black. So, it appears that the background of the user control ends up transparent.)
Trying to debug this, I've added the following code around my background assignment:
'' 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
All of the message box results are as expected, but still, I get no background gradient. Anybody have any idea what's going on?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
愚蠢,愚蠢,愚蠢!!!!
所以,显然,通过不在这里设置 alpha 值:
我正在制作透明颜色。
愚蠢,愚蠢,愚蠢!!!这是工作代码:
STUPID, STUPID, STUPID!!!!!
So, apparently, by not setting the alpha values here:
I was making transparent colors.
STUPID, STUPID, STUPID!!! Here's the working code: