2533192b24
-pulito codice
100 lines
3.2 KiB
VB.net
100 lines
3.2 KiB
VB.net
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"
|
|
|
|
''' <summary>
|
|
''' Restiusce il nome derivato dalla cartella
|
|
''' </summary>
|
|
''' <param name="path"></param>
|
|
''' <param name="pathChar"></param>
|
|
''' <param name="fileEnding"></param>
|
|
''' <returns></returns>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Ricerca i file dizionario contente il tema
|
|
''' </summary>
|
|
''' <param name="fileEnding"></param>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Ricerca i temi presenti nel programma
|
|
''' </summary>
|
|
''' <param name="relativePath"></param>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Funzione che permette di selezionare e applicare il tema
|
|
''' </summary>
|
|
''' <param name="theme"></param>
|
|
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
|