Files
EgtDOORCreator/DataService/ThemesDataService.vb
T
2024-11-19 15:24:11 +01:00

86 lines
2.8 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Resources
Imports Microsoft.Windows
Public Class ThemesDataService
Private ReadOnly m_Themes As New ObservableCollection(Of ThemeM)()
Sub New()
ScanResources()
ScanDisk("Themes")
End Sub
#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
Private Sub ScanResources(ByVal Optional fileEnding As String = "Theme.xaml")
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 ThemeM() With {
.Name = GetNameFromPath(fileName),
.Path = "pack://application:,,,/WpfTheme;component/" & fileName
})
End If
Next
Next
End Sub
Private Sub ScanDisk(ByVal relativePath As String)
If Directory.Exists(AppDomain.CurrentDomain.BaseDirectory & relativePath) Then
Dim themeFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory & relativePath, "*Theme.xaml", SearchOption.AllDirectories)
For Each fileName In themeFiles
m_Themes.Add(New ThemeM() With {
.Name = GetNameFromPath(fileName, "\"c),
.Path = fileName
})
Next
End If
End Sub
Public Sub SetTheme(ByVal theme As ThemeM)
If theme Is Nothing Then
EgtUILib.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
Public Class ThemeM
Public Property Name As String
Public Property Path As String
End Class