0

这个链接中,有一个“打开最近的文件”的代码,除了我之外,似乎每个人都明白那里发生了什么。添加代码只有几行,下面我不明白。这里是什么FileOpenCore??我应该用什么代替它?

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

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

1 回答 1

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.
    }
}
于 2012-03-23T15:54:46.913 回答