Saturday, September 17, 2011

Change App Style Resources Dynamically in Silverlight

Mostly this is the same ways to changing the style dynamically.
Like :

AddHandler element.MouseEnter, Sub(i, j)
                                                 i.Style = Application.Current.Resources("MouseHoverStyle")
                                             End Sub

AddHandler element.MouseLeave, Sub(i, j)                                               
                                                     i.Style = Application.Current.Resources("MouseLeaveStyle")   
                                             End Sub

We can change the APP.xaml Resources too. here the Steps below:
1) Prepare the styles called as Red,blue,green..etc
2) Then Change App Styles Dynamically here the code below.
VB Code:

  Public Sub LoadResourceDictionary(xamlTheme As String)
        xamlTheme = "/app-path;component/Assets/" & xamlTheme & ".xaml"
        If String.IsNullOrEmpty(xamlTheme) Then
        End If
            Dim resource As New ResourceDictionary
            resource.Source = New Uri(xamlTheme, UriKind.Relative)

            '/* Master Page elements is not Affected Dynamically*/
            Dim Page As MainPage = DirectCast(App.Current.RootVisual, MainPage)
            Page.MainBlock.Style = App.Current.Resources("LayoutRootGridStyle")
            Page.SliderMenu.Style = App.Current.Resources("Slidermenubackcolor")
            Page.ContentBorder.Style = App.Current.Resources("MainContentBackground")
            Page.MenuBlock.Style = App.Current.Resources("Loginmenubackcolor")
            Page.loginBlock.Style = App.Current.Resources("Loginmenubackcolor")
            Page.pathleft.Template = App.Current.Resources("ButtonArrowLeft")
            Page.pathright.Template = App.Current.Resources("ButtonArrowRight")
            Slidermenubackcolor1.Style = App.Current.Resources("Slidermenubackcolor")
            Slidermenubackcolor.Style = App.Current.Resources("Slidermenubackcolor")
            ContentSubBorder.Style = App.Current.Resources("ContentBorder")

            '/* Theme save in local path */
            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

            If Not Directory.Exists(path & "\app-path") Then
                Directory.CreateDirectory(path & "\app-path")
            End If
            If File.Exists(path & "\app-path\Setting.txt") Then
                File.Delete(path & "\app-path\Setting.txt")
            End If

            Dim sr As StreamWriter = New StreamWriter(File.Open(path & "\app-path\Setting.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            sr = Nothing
        Catch ex As Exception
            MessageBox.Show("Failed to load the theme " & vbLf + ex.Message)
        End Try
    End Sub

3) Prepare some simple border and Call while mouse event
    Private Sub rectred_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles red.MouseLeftButtonUp
    End Sub

    Private Sub rectblue_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Blue.MouseLeftButtonUp
    End Sub

4) Finally got change colors like that:

No comments:

Post a Comment