Frage

In this link, there is a code for "open recent files" and it seems that everybody understands whats going there except me. There are only a few lines to add the code and I dont understand below. Here what is FileOpenCore?? What should I replace for it?

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

partial class RecentFileList
{
   public void InsertFile( string filepath )
   public void RemoveFile( string filepath )
}
War es hilfreich?

Lösung

I believe FileOpenCore is the name the author gave to the method that actually opens your files. Replace it with whatever method you have that takes a file name and opens it.

The InsertFile method is to be called (probably in your FileOpenCore) whenever a file is successfully opened. The RemoveFile should be called if you attempted to open a file and it failed. You don't want to keep files that no longer exist in your recent files list, for example.

So, if you defined your RecentFileList as the author did:

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

And you hook up the click handler as he did in the constructor of your window:

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

Your FileOpenCore (or whatever you want to call it) might look something like this (pseudo-code):

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.
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top