私はかなり経験豊富な .NET デスクトップ開発者ですが、Silverlight は初めてです。iOS アプリケーションを Silverlight アプリに変換しようとしています。
このアプリは基本的に、データベースから取得したデータを使用して作成されたアイテムのリストです。このデータには、前景色と背景色の情報だけでなく、多数のラベル テキストが含まれています。各オブジェクトは、独自のユーザー コントロールです。これは、内部に Grid を持つ 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
すべてのメッセージ ボックスの結果は期待どおりですが、それでも背景のグラデーションは得られません。誰が何が起こっているのか知っていますか?
ありがとう!