VB2012:我已从这个网站http://www.codeproject.com/Articles/88564/MRU-Menu-Class-Revisited#xx4295995xx获取代码,为我的项目实现MRU可重用类。一切都很好,除了写入我修改它的特定XML文件只是使用My.Setttings命名空间。项目的加载工作正常,但保存只是不坚持,我认为它的属性被设置为ByVal。
在load事件中,我分配字符串集合mruList.FileList = My.Settings.MruInputFiles所以我认为这将是一个引用赋值。但是,当我去保存字符串集时,它似乎没有坚持。我通过手动添加My.Settings进行测试,但有效但是应该有一种方法通过类属性分配它吗?我只是没有得到我应该做的事情,只需设置类属性就能完成这项工作。
Public Class frmMain
Public WithEvents mruList As CMRmedia.MRUMenu
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mruList = New CMRmedia.MRUMenu(mnuLoadRecentInputs, Me)
'mruList.FileName = "x:\Path\To\My\File.xml"
mruList.FileList = My.Settings.MruInputFiles
mruList.MaxItems = My.Settings.MruMaxInputFiles
mruList.ShowClearRecent = True
mruList.Validate = True
mruList.StoreRelativePaths = False
'Loads the MRU list for persisted file if it exists
mruList.Load()
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
'save up the user settings
With My.Settings
'save the MRU list to persisted file before closing the application
mruList.Save()
.MruInputFiles = mruList.FileList 'this makes sure the file list gets assigned back to the settings
.Save()
End With
End Sub
End Class
Namespace CMRmedia
''' <summary>
''' Menu Class for automating handling of most recently used file menus
''' </summary>
Public Class MRUMenu
Private _MruList As New System.Collections.Generic.List(Of MruItem)
Private WithEvents _menuItem As ToolStripMenuItem
Private _mru As ToolStripMenuItem
'Private _FileName As String
Private _FileList As StringCollection
Private _MaxItems As Integer
''' <summary>
''' Get or Set the string collection for storing the MRU list
''' </summary>
Public Property FileList() As StringCollection
Get
Return _FileList
End Get
Set(ByVal value As StringCollection)
_FileList = value
End Set
End Property
''' <summary>
''' Saves the current MRU list to the MRU string collection
''' </summary>
''' <remarks></remarks>
Public Sub Save()
Try
'if the string collection has not been created then we create it here
If FileList Is Nothing Then FileList = New StringCollection
'clear the current contents of the string collection
FileList.Clear()
'now we add the contents of the menus to the string collection
Dim mnu As MruItem
For Each mnu In _MruList
Dim _path As String = mnu.FilePath
If _storeRelativePaths Then _path = transformPath(_path)
FileList.Add(_path)
Next mnu
'test to see if we manually assign to the My.Settings then it works.
'My.Settings.MruInputFiles = FileList
'My.Settings.MruMaxInputFiles = MaxItems
Catch ex As Exception
Dim e As New MruException
e.ErrorType = MruException.MruErrorType.FileListWriteError
e.innerException = ex.Message
e.message = "Error writing the MRU Settings"
RaiseEvent MruError(e)
End Try
End Sub
End Class
End Namespace