2012-03-23 66 views
0

this链接,有一个“打开最近的文件”的代码,似乎每个人都明白什么去除了我。只有几行添加代码,下面我不明白。这里是什么FileOpenCore ??我应该替换它什么?最近的文件列表

RecentFileList.MenuClick += (s, e) => FileOpenCore(e.Filepath); 

partial class RecentFileList 
{ 
    public void InsertFile(string filepath) 
    public void RemoveFile(string filepath) 
} 

回答

3

我相信FileOpenCore是作者给实际打开文件的方法的名称。用你拥有文件名的任何方法替换它并打开它。

只要文件成功打开,就会调用InsertFile方法(可能在您的FileOpenCore中)。如果您尝试打开文件并且失败,则应该调用RemoveFile。例如,您不想保留最近文件列表中不存在的文件。

所以,如果你定义了RecentFileList像笔者那样:

<common:RecentFileList x:Name="RecentFileList" /> 

你挂钩的点击处理程序,因为他在你的窗口的构造函数所做的:

RecentFileList.MenuClick += (s, e) => FileOpenCore(e.Filepath); 

你FileOpenCore(或任何你想叫它)可能看起来像这样(伪代码):

private void FileOpenCore(string filename) 
{ 
    try 
    { 
     // read your file 
     // and do whatever processing you need 
     // ... 
     // if open was successful 
     RecentFileList.InsertFile(filename); 
    } 
    catch (Exception e) 
    { 
     // opening the file failed - maybe it doesn't exist anymore 
     // or maybe it's corrupted 
     RecentFileList.RemoveFile(filename); 
     // Do whatever other error processing you want to do. 
    } 
} 
+0

它将菜单项添加到菜单但菜单点击事件不起作用 – Shibli 2012-03-23 17:28:40