XAML代码:
<MenuItem x:Name="documentaryMenuItem" Header="最近文档(_R)">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding PathName}" Click="Recent_Click"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
用ObservableCollection<T> 类保存打开的时间和文件路径;时间以字符串类型表示,取自DataTime.Now.Ticks,并转换为秒。保存在程序集的资源文件中,程序初始化时将资源文件内保存的文件打开时间及路径调入ObservableCollection。
using System;
using System.Collections.ObjectModel;
using System.Resources;
using System.Collections;
namespace XXX
{
public class RecentFiles:ObservableCollection<FileItem>
{
private string rn = "myRes.resources";//资源文件名
public int MaxCount;
public RecentFiles(int max=5) : base()
{
MaxCount = max;//显示最近打开文件的最大个数
CollectionChanged += RecentFile_CollectionChanged;
}
public new void Add(FileItem fi)
{
int ctr = 0;
if (this.Contains(fi))//文件名集合中是否包含了需增加的元素
{
ctr=this.IndexOf(fi);
this.RemoveItem(ctr);//集合中移除已存在的元素
}
else{
if (this.Count >= MaxCount) this.RemoveItem(ctr);//个数大于等于MaxCount,删除第一个
}
base.Add(fi);//新增加到末尾
}
private void RecentFile_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//ToDo Something;
}
public void Load()
{//调入存储的最近打开的文件名
try
{
ResourceReader rr = new ResourceReader(rn);
IDictionaryEnumerator dict = rr.GetEnumerator();
int ctr = 0;
while (dict.MoveNext())
{
ctr++;
this.Add(new FileItem(dict.Key.ToString(), dict.Value.ToString()));
}
catch(Exception e){}
}
public void Cache()
{//将集合中的文件名存入资源
ResourceWriter rw = new ResourceWriter(rn);
foreach (var item in this)
{
rw.AddResource(item.TimeStamp, item.PathName);
}
rw.Close();
}
}
public class FileItem: IEquatable<FileItem>
{
public string TimeStamp
{
get{return timeStamp;}
set { timeStamp = value; }
}
public string PathName
{
get { return pathName; }
set { pathName = value; }
}
private string timeStamp;
private string pathName;
public FileItem(string timeStamp,string pathName)
{
this.TimeStamp = timeStamp;
this.PathName = pathName;
}
public bool Equals(FileItem other)
{//确定相等性的方法,仅比较文件名,集合方法Contains的比较依据
if (other == null)return false;
if(this.pathName==other.PathName)
{
return true;
}
else
{
return false;
}
}
}
}
以下代码为数据绑定部分,以时间为关键字降序排列:
private RecentFiles PathsCollection =new RecentFiles();
documentaryMenuItem.ItemsSource = PathsCollection;
documentaryMenuItem.Items.SortDescriptions.Add(new SortDescription("TimeStamp", ListSortDirection.Descending));
文件信息写入ObservableCollection:
string TimeStamp = new TimeSpan(DateTime.Now.Ticks).TotalSeconds.ToString();
PathsCollection.Add(new FileItem(TimeStamp, ImgFilePath.ToString()));
程序退出时保存文件信息集合到资源文件:
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
string msg = "是否退出?";
string title = "退出";
MessageBoxButton buttons = MessageBoxButton.YesNo;
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBoxResult result = MessageBox.Show(msg, title, buttons, icon);
e.Cancel = (result == MessageBoxResult.No);
if (result == MessageBoxResult.Yes)
{
PathsCollection.Cache();
}
}
读取菜单Header字符获得文件路径:
private void Recent_Click(object sender, RoutedEventArgs e)
{
MenuItem menu = sender as MenuItem;
string fn = menu.Header.ToString();//取得文件名
//ToDo Something;
}
实际运行结果: