VB2012:我已从该站点http://www.codeproject.com/Articles/88564/MRU-Menu-Class-Revisited#xx4295995xx获取代码,为我的项目实现 MRU 可重用类。除了写入特定的 XML 文件之外,一切都很好,我将其修改为仅使用 My.Setttings 命名空间。项目的加载工作正常,但保存并没有坚持,我认为这是因为属性设置为 ByVal。
在加载事件中,我分配了字符串集合 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