⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settings.vb

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 VB
字号:
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Drawing
Imports System.Collections.Specialized

Public Class Settings
    Private Shared ReadOnly settingsFileName As String = "MobileDeveloperHandbookSettings.xml"

    ''' <summary>
    ''' Static private member stores ref to single instance of this class
    ''' </summary>
    Private Shared thisClass As Settings = Nothing

    Public Shared ReadOnly Property Instance() As Settings
        Get
            Return Settings.GetSettings()
        End Get
    End Property

    ''' <summary>
    ''' Static method returns single instance of the settings class.
    ''' First time this method is called it will load the settings class
    ''' from the persistance file, if one exists.
    ''' </summary>
    ''' <returns>single instance of the Settings class</returns>
    Private Shared Function GetSettings() As Settings
        If Settings.thisClass Is Nothing Then
            ' Create instance of the class
            Settings.thisClass = New Settings()

            ' Load from persistence file (if one exists)
            Dim _path As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

            Dim haveSettings As Boolean = False
            If File.Exists(_path + "\" + settingsFileName) Then
                Dim serializer As XmlSerializer = XmlSerializerCache.GetXmlSerializer(GetType(Settings), "http://tempuri.org/")
                Dim reader As TextReader = New StreamReader(_path + "\" + settingsFileName)
                Try
                    Dim [myClass] As Settings = DirectCast(serializer.Deserialize(reader), Settings)
                    Settings.thisClass = [myClass]
                    haveSettings = True
                Catch ex As InvalidOperationException
                    System.Windows.Forms.MessageBox.Show("Error reading Settings file: " + ex.Message)
                End Try
                reader.Close()
            End If

            If Not haveSettings Then
                ' No persistance file - initialise settings with defaults
                Settings.thisClass.settingsHashTable("FontName") = "Nina"
                Settings.thisClass.settingsHashTable("FontSize") = 11
                Settings.thisClass.settingsHashTable("BackgroundColor") = Color.Black
                Settings.thisClass.settingsHashTable("TextColor") = Color.White
            End If
        End If
        Return Settings.thisClass
    End Function

    ''' <summary>
    ''' Save the settings to the persistance file
    ''' </summary>
    Public Shared Sub SaveSettings()
        Dim serializer As XmlSerializer = XmlSerializerCache.GetXmlSerializer(GetType(Settings), "http://tempuri.org/")
        Dim _path As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
        Dim writer As TextWriter = New StreamWriter(_path + "\" + settingsFileName, False)
        serializer.Serialize(writer, Settings.Instance)
        writer.Close()
    End Sub

#Region "Methods"
    ''' <summary>
    ''' default constructor for serialization
    ''' </summary>
    Public Sub New()
    End Sub

    Public Function SerializeColor(ByVal color As Color) As String
        Return String.Format("{0}:{1}:{2}", color.R, color.G, color.B)
    End Function

    Public Function DeserializeColor(ByVal colorToConvert As String) As Color
        Dim r As Integer, g As Integer, b As Integer

        Dim pieces As String() = colorToConvert.Split(New Char() {":"c})

        r = Integer.Parse(pieces(0))
        g = Integer.Parse(pieces(1))
        b = Integer.Parse(pieces(2))

        Return Color.FromArgb(r, g, b)
    End Function
#End Region

#Region "Properties"
    ''' <summary>
    ''' stores the individual settings
    ''' </summary>
    <XmlIgnore()> _
    Private settingsHashTable As New ListDictionary()

    ''' <summary>
    ''' Font for text display
    ''' </summary>
    Public Property Fontname() As String
        Get
            Return DirectCast(settingsHashTable("FontName"), String)
        End Get
        Set(ByVal value As String)
            settingsHashTable("FontName") = value
        End Set
    End Property

    ''' <summary>
    ''' Size of font used for display
    ''' </summary>
    Public Property FontSize() As Integer
        Get
            Return CInt(settingsHashTable("FontSize"))
        End Get
        Set(ByVal value As Integer)
            settingsHashTable("FontSize") = value
        End Set
    End Property

    ''' <summary>
    ''' color for background
    ''' </summary>
    <XmlIgnore()> _
    Public Property BackgroundColor() As System.Drawing.Color
        Get
            Return DirectCast(settingsHashTable("BackgroundColor"), Color)
        End Get
        Set(ByVal value As System.Drawing.Color)
            settingsHashTable("BackgroundColor") = value
        End Set
    End Property
    <XmlElement("BackgroundColor")> _
    Public Property XmlBackgroundColor() As String
        Get
            Return Settings.Instance.SerializeColor(BackgroundColor)
        End Get
        Set(ByVal value As String)
            BackgroundColor = Settings.Instance.DeserializeColor(value)
        End Set
    End Property

    ''' <summary>
    ''' color for text
    ''' </summary>
    <XmlIgnore()> _
    Public Property TextColor() As System.Drawing.Color
        Get
            Return DirectCast(settingsHashTable("TextColor"), Color)
        End Get
        Set(ByVal value As System.Drawing.Color)
            settingsHashTable("TextColor") = value
        End Set
    End Property
    <XmlElement("TextColor")> _
    Public Property XmlTextColor() As String
        Get
            Return Settings.Instance.SerializeColor(TextColor)
        End Get
        Set(ByVal value As String)
            TextColor = Settings.Instance.DeserializeColor(value)
        End Set
    End Property

#End Region
End Class

Public Class XmlSerializerCache
#Region "fields"
    Private Shared table As ListDictionary
#End Region

#Region "constructors"
    Shared Sub New()
        XmlSerializerCache.table = New ListDictionary()
    End Sub

    Private Sub New()
    End Sub
#End Region

    Public Shared Function GetXmlSerializer(ByVal type As Type, ByVal defaultNamespace As String) As XmlSerializer
        Dim serializer As XmlSerializer

        If type Is Nothing Then
            Throw New ArgumentNullException("type")
        End If

        ' Make it thread safe
        SyncLock XmlSerializerCache.table.SyncRoot
            Dim typeName As String
            If (defaultNamespace Is Nothing) OrElse (defaultNamespace.Length = 0) Then
                typeName = type.FullName + "#"
            Else
                typeName = type.FullName + "#" + defaultNamespace
            End If

            ' Try to get the serializer from cache
            Dim obj As Object = XmlSerializerCache.table(typeName)
            If obj Is Nothing Then
                ' We don't have it, create a new instance 
                obj = New XmlSerializer(type, defaultNamespace)
                XmlSerializerCache.table.Add(typeName, obj)
            End If
            serializer = TryCast(obj, XmlSerializer)
        End SyncLock
        Return serializer
    End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -