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:
- Public Shared Function GetCurrentTheme() As Theme
-      Dim bgc = App.Current.Resources( "PhoneBackgroundColor" ).ToString()
-      If bgc = "#FF000000" Then
-          Return Theme .Dark
- Else
- Return Theme . Light
- End If End
- Function
Theme enumeration is defined as follows:
- Public Enum Theme
- Dark
- Light
- End Enum
At this point we can create our conveter:
- Imports System.Windows.Data
- Imports System.Windows.Media
- Public Class ThemeColorConverter
- Implements IValueConverter
- Private Function GetColorFromName( ByVal strColor As String ) As Color ?
-          Dim retColor As Color ? = Nothing
-          Try
-              Dim propColor = GetType ( Colors ).GetProperty(strColor)
-              If propColor IsNot Nothing Then
-                  Dim value = propColor .GetValue( Nothing , Nothing )
-                  If value IsNot Nothing Then
-                     retColor = CType (value, Color )
-                  End If
-              End If
-          Catch ex As Exception
-             retColor = Nothing
-          End Try
-          Return retColor
-      End Function
-
-      Private Function GetColorFromRGB( ByVal strColor As String ) As Color ?
-          Dim retColor As Color ? = Nothing
-         strColor = strColor .Replace( "#" , "" )
-          Dim a As Byte = 255
-          Dim r As Byte = 255
-          Dim g As Byte = 255
-          Dim b As Byte = 255
-          Dim start = 0
-          If strColor .Length = 6 Or strColor .Length = 8 Then
-              If strColor .Length = 8 Then
-                 a = Byte .Parse( strColor .Substring(0, 2), System.Globalization. NumberStyles .HexNumber)
-                 start = 2
-              End If
-             r = Byte .Parse( strColor .Substring(start, 2), System.Globalization. NumberStyles .HexNumber)
-             g Bytes = . Parse ( strColor . Substring (start + 2, 2), System.Globalization. NumberStyles . HexNumber)
- & # 160; b = Byte . Parse ( strColor . Substring (start + 4, 2) , System.Globalization. NumberStyles . HexNumber)
- & # 160; retColor = Color .FromArgb(a, r, g, b)
-          End If
-          Return retColor
-      End Function
-
-      Private Function GetColor( ByVal strColor As String ) As Color
-          Dim retColor As Color
-
-          Dim tmpColor = GetColorFromName(strColor)
-          If Not tmpColor .HasValue Then
-             tmpColor = GetColorFromRGB(strColor)
-              If tmpColor .HasValue Then
-                 retColor = tmpColor .Value
-              End If
-          Else
-             retColor = tmpColor .Value
-          End If
-
-          Return retColor
-      End Function
-
-      Public Function Convert( ByVal value As Object ,
-                              ByVal targetType As System. Type ,
-                              ByVal parameter As Object ,
-                              ByVal culture As System.Globalization. CultureInfo ) As Object Implements System.Windows.Data. IValueConverter .Convert
-          If value IsNot Nothing Then
-              Dim brush As SolidColorBrush = Nothing
- ,.. Dim values \u200b\u200b= value ToString () Split ( " If
- App .GetCurrentTheme() = Theme .Light Then
-                     brush = New SolidColorBrush (GetColor(values(0)))                  Else
-                     brush = New SolidColorBrush (GetColor(values(1)))
-                  End
- If              End
- If              Return brush
-          Else             
- Return Nothing
-          End
- If      End
- Function     
- Public Function ConvertBack( ByVal value
- As
- Object ,                                  ByVal targetType As System. Type ,                                 
- ByVal parameter As Object ,                                 
- ByVal culture As System.Globalization. CultureInfo ) As
- Object Implements System.Windows.Data. IValueConverter .ConvertBack          Throw New NotImplementedException ()     
- End Function End
- Class
- 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 ). The Convert method of our converter retrieves the argument value, which by convention should be a string with the following format:
To do this you simply reference the appropriate namespace:
xmlns:
my = "clr-namespace: WP7ThemeBackground"
- Insert the converter into the resources of 'application or page
- \u0026lt; phone
>
- \u0026lt; my : ThemeColorConverter x :
- Key = "TCC"> \u0026lt;/ my : ThemeColorConverter > \u0026lt;/ phone : PhoneApplicationPage.Resources >
- And finally, put the drive in with the background binding of control desired: \u0026lt; Grid
Name = "LayoutRoot" Class
- Background = "{Binding Converter = { StaticResource TCC} , Source = Red \u0026lt; phone : phone application page x
= "WP7ThemeBackground.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- ; xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns: phone = "clr-namespace: Microsoft.Phone.Controls; Microsoft.Phone assembly ="
- ; xmlns:
- shell = "clr-namespace: Microsoft.Phone.Shell; Microsoft.Phone assembly =" & # 160; xmlns:
- d = "http://schemas.microsoft.com/expression/blend/2008"     xmlns :
- mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns :
- my ="clr-namespace:WP7ThemeBackground"     mc :
- Ignorable ="d" d : DesignWidth
- ="480" d : design height = "768"
- FontFamily = "{ StaticResource phone FontFamily normal} " font size = "{ StaticResource phone font size normal} "
- Foreground = "{ StaticResource PhoneForegroundBrush} "    
- SupportedOrientations ="Portrait" Orientation ="Portrait"     shell
- : SystemTray.IsVisible ="True" >      <
- phone : PhoneApplicationPage.Resources >         
- < my : ThemeColorConverter
- x : Key ="TCC"></ my : ThemeColorConverter
- >      </ phone : PhoneApplicationPage.Resources >      <!--LayoutRoot is the root grid where all page content is placed-->      <
- Grid x : Name ="LayoutRoot" Background ="{
- Binding Converter ={
- StaticResource TCC} , Source =Red AA00FF00} "> \u0026lt; Grid. RowDefinitions > & # 160; \u0026lt; RowDefinition Height = "Auto" /> <
- RowDefinition Height ="*"/>         
- </ Grid.RowDefinitions >         
- <!--TitlePanel contains the name of the application and page title--> \u0026lt; StackPanel x
- : Name = "title panel" Grid.Row = "0"
- Margin
- = "12,17,0,28> & # 160;
- \u0026lt; TextBlock x : Name = "application title" text = "My Application" Style = "{ StaticResource phone text normal style}
- "/> & # 160; \u0026lt; TextBlock x : name = "page title" text = "pagename" Margin = "9, -7,0,0" Style = "{
- StaticResource PhoneTextTitle1Style } "/> \u0026lt;/ StackPanel > \u0026lt;- content panel - place additional content here -> \u0026lt; Grid x
- : Name = "content panel" Grid.Row = "1"
- Margin
- = "12,0,12,0"> \u0026lt;/ Grid>
- \u0026lt;/ Grid> \u0026lt;/ phone: PhoneApplicationPage >
- Tag on Technorati: IValueConverter , WP7
- , binding , phone windows 7 , background , solidbrush , color, colors
0 comments:
Post a Comment