Saturday, January 8, 2011

Wrestling Singlets For Baby

WP7 Tip: Changing the background of a control based on the theme

Following a thread appeared on forum for the development in Microsoft Windows phone I will return an implementation of a converter that allows you to change the background of a control in XAML based on the active theme.

Getting Started "steal" the tip of the evil genius on the way to determine the active theme (link ) and convert it to VB.NET. In particular, we create a shared method (but should be a property shared well) in our application class App:

  1. Public Shared Function GetCurrentTheme() As Theme
  2.      Dim bgc = App.Current.Resources( "PhoneBackgroundColor" ).ToString()
  3.      If bgc = "#FF000000" Then
  4.          Return Theme .Dark
  5. Else
  6. Return Theme .
  7. Light
  8. End If End
  9. Function

Theme enumeration is defined as follows:

  1. Public Enum Theme
  2. Dark
  3. Light
  4. End Enum

At this point we can create our conveter:

  1. Imports System.Windows.Data
  2. Imports System.Windows.Media
  3. Public Class ThemeColorConverter
  4. Implements IValueConverter
  5. Private Function GetColorFromName( ByVal strColor As String ) As Color ?
  6.          Dim retColor As Color ? = Nothing
  7.          Try
  8.              Dim propColor = GetType ( Colors ).GetProperty(strColor)
  9.              If propColor IsNot Nothing Then
  10.                  Dim value = propColor .GetValue( Nothing , Nothing )
  11.                  If value IsNot Nothing Then
  12.                     retColor = CType (value, Color )
  13.                  End If
  14.              End If
  15.          Catch ex As Exception
  16.             retColor = Nothing
  17.          End Try
  18.          Return retColor
  19.      End Function
  20.  
  21.      Private Function GetColorFromRGB( ByVal strColor As String ) As Color ?
  22.          Dim retColor As Color ? = Nothing
  23.         strColor = strColor .Replace( "#" , "" )
  24.          Dim a As Byte = 255
  25.          Dim r As Byte = 255
  26.          Dim g As Byte = 255
  27.          Dim b As Byte = 255
  28.          Dim start = 0
  29.          If strColor .Length = 6 Or strColor .Length = 8 Then
  30.              If strColor .Length = 8 Then
  31.                 a = Byte .Parse( strColor .Substring(0, 2), System.Globalization. NumberStyles .HexNumber)
  32.                 start = 2
  33.              End If
  34.             r = Byte .Parse( strColor .Substring(start, 2), System.Globalization. NumberStyles .HexNumber)
  35.             g Bytes = . Parse ( strColor . Substring (start + 2, 2), System.Globalization. NumberStyles . HexNumber)
  36. & # 160; b = Byte . Parse ( strColor . Substring (start + 4, 2) , System.Globalization. NumberStyles . HexNumber)
  37. & # 160; retColor = Color .FromArgb(a, r, g, b)
  38.          End If
  39.          Return retColor
  40.      End Function
  41.  
  42.      Private Function GetColor( ByVal strColor As String ) As Color
  43.          Dim retColor As Color
  44.  
  45.          Dim tmpColor = GetColorFromName(strColor)
  46.          If Not tmpColor .HasValue Then
  47.             tmpColor = GetColorFromRGB(strColor)
  48.              If tmpColor .HasValue Then
  49.                 retColor = tmpColor .Value
  50.              End If
  51.          Else
  52.             retColor = tmpColor .Value
  53.          End If
  54.  
  55.          Return retColor
  56.      End Function
  57.  
  58.      Public Function Convert( ByVal value As Object ,
  59.                              ByVal targetType As System. Type ,
  60.                              ByVal parameter As Object ,
  61.                              ByVal culture As System.Globalization. CultureInfo ) As Object Implements System.Windows.Data. IValueConverter .Convert
  62.          If value IsNot Nothing Then
  63.              Dim brush As SolidColorBrush = Nothing
  64. ,.. Dim values \u200b\u200b= value ToString () Split ( " If
  65. App .GetCurrentTheme() = Theme .Light Then
  66.                     brush = New SolidColorBrush (GetColor(values(0)))                  Else
  67.                     brush = New SolidColorBrush
  68. (GetColor(values(1)))
  69.                  End
  70. If              End
  71. If              Return
  72. brush
  73.          Else
  74.             
  75. Return Nothing
  76.          End
  77. If      End
  78. Function  
  79.     
  80. Public Function ConvertBack( ByVal
  81. value
  82. As
  83. Object ,                                  ByVal targetType As System. Type ,
  84.                                 
  85. ByVal parameter As Object ,
  86.                                 
  87. ByVal culture As System.Globalization. CultureInfo ) As
  88. Object Implements System.Windows.Data. IValueConverter .ConvertBack          Throw New NotImplementedException ()
  89.     
  90. End Function End
  91. Class
  92. La funzione GetColor ci consente di obtain a Color object from a string in the following formats AARRGGBB #, # RRGGBB or "color name" (as that may be one of the possible colors of the class Colors ).
  93. The Convert method of our converter retrieves the argument value, which by convention should be a string with the following format:
light color theme can be used directly in binding within the XAML.

To do this you simply reference the appropriate namespace:

xmlns:

my = "clr-namespace: WP7ThemeBackground"

    Insert the converter into the resources of 'application or page
  1. \u0026lt; phone
:
PhoneApplicationPage.Resources

>

  1. \u0026lt; my : ThemeColorConverter x :
  2. Key = "TCC"> \u0026lt;/ my : ThemeColorConverter > \u0026lt;/ phone : PhoneApplicationPage.Resources >
  3. And finally, put the drive in with the background binding of control desired: \u0026lt; Grid
x
:

Name = "LayoutRoot"
  1. Background = "{Binding Converter = { StaticResource TCC} , Source = Red \u0026lt; phone : phone application page x
:
Class

= "WP7ThemeBackground.MainPage"

xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  1. ; xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  2. xmlns: phone = "clr-namespace: Microsoft.Phone.Controls; Microsoft.Phone assembly ="
  3. ; xmlns:
  4. shell = "clr-namespace: Microsoft.Phone.Shell; Microsoft.Phone assembly =" & # 160; xmlns:
  5. d = "http://schemas.microsoft.com/expression/blend/2008"     xmlns :
  6. mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns :
  7. my ="clr-namespace:WP7ThemeBackground"     mc :
  8. Ignorable ="d" d : DesignWidth
  9. ="480" d : design height = "768"
  10. FontFamily = "{ StaticResource phone FontFamily normal} " font size = "{ StaticResource phone font size normal} "
  11. Foreground = "{ StaticResource PhoneForegroundBrush} "
  12.    
  13. SupportedOrientations ="Portrait" Orientation ="Portrait"     shell
  14. : SystemTray.IsVisible ="True" >      <
  15. phone : PhoneApplicationPage.Resources >
  16.         
  17. < my : ThemeColorConverter
  18. x : Key ="TCC"></ my : ThemeColorConverter
  19. >      </ phone : PhoneApplicationPage.Resources >      <!--LayoutRoot is the root grid where all page content is placed-->      <
  20. Grid x : Name ="LayoutRoot" Background ="{
  21. Binding Converter ={
  22. StaticResource TCC} , Source =Red AA00FF00} "> \u0026lt; Grid. RowDefinitions > & # 160; \u0026lt; RowDefinition Height = "Auto" />
  23. <
  24. RowDefinition Height ="*"/>         
  25. </ Grid.RowDefinitions >           
  26. <!--TitlePanel contains the name of the application and page title--> \u0026lt; StackPanel
  27. x
  28. : Name = "title panel" Grid.Row
  29. = "0"
  30. Margin
  31. = "12,17,0,28> & # 160;
  32. \u0026lt; TextBlock x : Name = "application title" text = "My Application" Style = "{ StaticResource
  33. phone text normal style}
  34. "/> & # 160; \u0026lt; TextBlock x : name = "page title" text = "pagename" Margin = "9, -7,0,0"
  35. Style = "{
  36. StaticResource PhoneTextTitle1Style } "/> \u0026lt;/ StackPanel > \u0026lt;- content panel - place additional content here -> \u0026lt; Grid
  37. x
  38. : Name = "content panel" Grid.Row
  39. = "1"
  40. Margin
  41. = "12,0,12,0"> \u0026lt;/
  42. Grid>
  43. \u0026lt;/ Grid> \u0026lt;/ phone: PhoneApplicationPage >
  44. Tag on Technorati: IValueConverter ,
  45. WP7
  46. , binding , phone windows 7 , background , solidbrush , color, colors

0 comments:

Post a Comment