Imports System.Collections.ObjectModel Imports System.IO Imports System.Resources Imports EgtUILib Public Class ThemesDataServiceVM #Region "FIELDS & PROPERTIES" Private ReadOnly m_Themes As New ObservableCollection(Of ThemesDataServiceM)() #End Region ' Fields & Properties #Region "CONSTRUCTOR" Sub New() ScanResources() ScanDisk(UTILITY_FOLDER) End Sub #End Region ' Constructor #Region "METHODS" ''' ''' Restiusce il nome derivato dalla cartella ''' ''' ''' ''' ''' Private Function GetNameFromPath(ByVal path As String, ByVal Optional pathChar As Char = "/"c, ByVal Optional fileEnding As String = "Theme.xaml") As String Dim name As String = path.Substring(path.LastIndexOf(pathChar) + 1) name = name.Substring(0, name.Length - fileEnding.Length) name = Char.ToUpper(name(0)) & If(name.Length > 1, name.Substring(1), "") Return name End Function ''' ''' Ricerca i file dizionario contente il tema ''' ''' Private Sub ScanResources(ByVal Optional fileEnding As String = DICTIONARY_FILENAME) Dim assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim resourceNames = assembly.GetManifestResourceNames() For Each resourceName In resourceNames Dim [set] As New ResourceSet(assembly.GetManifestResourceStream(resourceName)) For Each item As DictionaryEntry In [set] Dim fileName As String = item.Key.ToString() If fileName.ToLower().EndsWith(fileEnding.ToLower()) Then m_Themes.Add(New ThemesDataServiceM() With { .Name = GetNameFromPath(fileName), .Path = "pack://application:,,,/WpfTheme;component/" & fileName }) End If Next Next End Sub ''' ''' Ricerca i temi presenti nel programma ''' ''' Private Sub ScanDisk(ByVal relativePath As String) If Directory.Exists(AppDomain.CurrentDomain.BaseDirectory & relativePath) Then Dim themeFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory & relativePath, "*" & DICTIONARY_FILENAME, SearchOption.AllDirectories) For Each fileName In themeFiles m_Themes.Add(New ThemesDataServiceM() With { .Name = GetNameFromPath(fileName, "\"c), .Path = fileName }) Next End If End Sub ''' ''' Funzione che permette di selezionare e applicare il tema ''' ''' Public Sub SetTheme(ByVal theme As ThemesDataServiceM) If theme Is Nothing Then EgtOutLog("Error setting theme: Attempting to set theme to null.") Return End If Try Application.Current.Resources.MergedDictionaries(1).Source = New Uri(theme.Path, UriKind.RelativeOrAbsolute) Catch ex As Exception EgtUILib.EgtOutLog("Error setting theme: " & ex.Message) End Try End Sub #End Region ' Methods End Class