我在这里再次尝试用我在另一个问题ArrayList
中解释过的问题来解决这个问题......不同之处在于这次我没有寻找任何替代解决方案,例如使用其他外部文件来避免真正的问题,我真的很想使用My.Settings和ArrayList来解决这个问题,我想了解这里发生的关于My.Settings的事情!
问题是,如果我们设置这样的设置:
然后在下一个应用程序运行时保留对设置执行的任何更改,此代码演示了该问题:
Public Class Test
Private Sub Test_Handler() Handles MyBase.Shown
' Create a temporal predefined ArrayList.
Dim tmpArrayList As New ArrayList(capacity:=10I)
With tmpArrayList
.Add({"Item0", 0.0F})
.Add({"Item1", 0.5F})
End With
' Check the setting status.
If My.Settings.MRU Is Nothing Then
Debug.WriteLine("MRU setting is null.")
Debug.WriteLine("Initializing the Setting...")
My.Settings.MRU = New ArrayList(capacity:=10I)
ElseIf My.Settings.MRU.Count = 0 Then
Debug.WriteLine("MRU is not null but the ArrayList is empty.")
Debug.WriteLine("Adding some items...")
My.Settings.MRU = tmpArrayList.Clone
ElseIf My.Settings.MRU.Count > 0 Then ' This part of the block will never thrown.
Debug.WriteLine("MRU setting is OK.")
Debug.WriteLine("Item Count: " & CStr(My.Settings.MRU.Count))
Threading.Thread.Sleep(Integer.MaxValue)
End If
Debug.WriteLine("Saving any changes")
My.Settings.Save()
Debug.WriteLine("Updating any changes")
My.Settings.Reload()
Debug.WriteLine(String.Empty)
Debug.WriteLine("****************************************")
Debug.WriteLine("Checking again the MRU setting status in...")
For Count As Integer = 1 To 3
Debug.WriteLine(CStr(Count) & New String("."c, Count))
Threading.Thread.Sleep(TimeSpan.FromSeconds(1))
Next
Debug.WriteLine("****************************************")
Debug.WriteLine(String.Empty)
Me.Test_Handler()
End Sub
End Class
有人可以教我理解为什么 ArrayList 没有真正保存,或者告诉我如何解决这个问题?
更新
好的,我编写了这个可序列化的类来解决这个问题:
''' <summary>
''' A Class intended to use it as an Item for a MRU item collection that stores the item filepath, with additional info.
''' </summary>
<Serializable()>
Public Class MostRecentUsedItem
''' <summary>
''' Gets or sets the item filepath.
''' </summary>
''' <value>The file path.</value>
Public Property FilePath As String
''' <summary>
''' (Optionally) Gets or sets the item index.
''' </summary>
''' <value>The index.</value>
Public Property Index As Integer
''' <summary>
''' (Optionally) Gets or sets the item image.
''' </summary>
''' <value>The image.</value>
Public Property Img As Bitmap
''' <summary>
''' (Optionally) Gets or sets the item last-time open date.
''' </summary>
''' <value>The index.</value>
Public Property [Date] As Date
''' <summary>
''' (Optionally) Gets or sets the item tag.
''' </summary>
''' <value>The tag object.</value>
Public Property Tag As Object
End Class
我还编写了这个辅助函数来帮助我解决这个问题:
''' <summary>
''' Determines whether an object can be XML serialized.
''' </summary>
''' <param name="Object">The object.</param>
''' <returns><c>true</c> if object is XML serializable; otherwise, <c>false</c>.</returns>
Private Function IsObjectSerializable(ByVal [Object] As Object) As Boolean
Using fs As New IO.FileStream(IO.Path.GetTempFileName, IO.FileMode.Create)
Dim Serializer As New Xml.Serialization.XmlSerializer([Object].GetType)
Try
Serializer.Serialize(fs, [Object])
Return True
Catch ex As InvalidOperationException
Return False
End Try
End Using
End Function
在我这样初始化设置的那一刻,它是可序列化的:
My.Settings.MRU = New ArrayList
在我只添加一个字符串的那一刻,它仍然是可序列化的:
My.Settings.MRU.Add("test string")
但是当我尝试添加我的可序列化类或任何其他类型的数据类型String()
时,ArrayList 开始不可序列化,就像这样:
My.Settings.MRU.Add({"Collection", "Of", "Strings"})
或者像这样:
Dim MRUItem As New MostRecentUsedItem
MRUItem.FilePath = "C:\Test.ext"
My.Settings.MRU.Add(MRUItem)
...因此 ArrayList 内容不会在下次运行时保留,无法序列化。
我还尝试将设置类型从更改System.Collections.ArrayList
为System.Object
(拼命地),所以现在我可以这样做,但问题仍然存在,我的意思是在下一次运行应用程序时不会保存该集合:
My.Settings.MRU = New List(Of MostRecentUsedItem)
Dim MRUItem As New MostRecentUsedItem
MRUItem.FilePath = "C:\Test.ext"
My.Settings.MRU.Add(MRUItem)