xamarin.Android C# - IFileNameFilter class 实现问题
xamarin.Android C# - IFileNameFilter class implementation issues
在我的文件资源管理器应用程序中,我试图在每次打开文件夹时仅显示任何文件夹中的 xml 个文件 present.So,我只想显示 xml files.I 浏览了这个网站和互联网,有很多示例,但在 C# 中没有找到。我也尝试修改其他示例以满足我的需要,但我无法实现 IFileNameFilter class.Does 有人知道怎么做吗?我试图实现它,但不断出现构建错误。
在我下面的代码中,我有一个显示文件的 ListFragment class:
public override void OnListItemClick(ListView l, View v, int position, long id)
{
try{
var fileSystemInfo = _adapter.GetItem(position);
if (fileSystemInfo.IsFile())
{
// Do something with the file. In this case we just pop some toast.
Log.Verbose("FileListFragment", "The file {0} was clicked.", fileSystemInfo.FullName);
Toast.MakeText(Activity, "You selected file " + fileSystemInfo.FullName, ToastLength.Short).Show();
OnFileClick (fileSystemInfo);
}
else
{
// Dig into this directory, and display it's contents
RefreshFilesList(fileSystemInfo.FullName);
}
}catch(Exception e){
Log.Error (TAG,e.ToString());
}
base.OnListItemClick(l, v, position, id);
}
public void RefreshFilesList(string directory)
{
//GenericExtFilter filter = new GenericExtFilter(extension);
IList<FileSystemInfo> visibleThings = new List<FileSystemInfo>();
var dir = new DirectoryInfo(directory);
try
{
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
visibleThings.Add(item);
}
}
catch (Exception ex)
{
Log.Error("FileListFragment", "Couldn't access the directory " + _directory.FullName + "; " + ex);
Toast.MakeText(Activity, "Problem retrieving contents of " + directory, ToastLength.Long).Show();
return;
}
_directory = dir;
_adapter.AddDirectoryContents(visibleThings);
// If we don't do this, then the ListView will not update itself when the data set
// in the adapter changes. It will appear to the user that nothing has happened.
ListView.RefreshDrawableState();
Log.Verbose("FileListFragment", "Displaying the contents of directory {0}.", directory);
}
如果我理解正确的话,您想创建 IFilenameFilter 来过滤 XML 个文件。
public class XmlFileFilter : Java.Lang.Object, IFilenameFilter
{
public bool Accept(File dir, string filename)
{
return filename.ToLower().EndsWith(".xml");
}
}
我用更简单的方法解决了这个问题 solution.I 在 all.Inside 上面的 RefreshFilesList 方法之后没有使用 IFileNameFilter,我做了以下更改:
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
if(item.IsDirectory())
{
visibleThings.Add(item);
}else if(item.IsFile())
{
bool isXmlFile = item.Extension.ToLower().EndsWith("xml");
if(isXmlFile)
{
visibleThings.Add(item);
}
}
}
现在显示所有文件夹和任何 xml 文件。
在我的文件资源管理器应用程序中,我试图在每次打开文件夹时仅显示任何文件夹中的 xml 个文件 present.So,我只想显示 xml files.I 浏览了这个网站和互联网,有很多示例,但在 C# 中没有找到。我也尝试修改其他示例以满足我的需要,但我无法实现 IFileNameFilter class.Does 有人知道怎么做吗?我试图实现它,但不断出现构建错误。
在我下面的代码中,我有一个显示文件的 ListFragment class:
public override void OnListItemClick(ListView l, View v, int position, long id)
{
try{
var fileSystemInfo = _adapter.GetItem(position);
if (fileSystemInfo.IsFile())
{
// Do something with the file. In this case we just pop some toast.
Log.Verbose("FileListFragment", "The file {0} was clicked.", fileSystemInfo.FullName);
Toast.MakeText(Activity, "You selected file " + fileSystemInfo.FullName, ToastLength.Short).Show();
OnFileClick (fileSystemInfo);
}
else
{
// Dig into this directory, and display it's contents
RefreshFilesList(fileSystemInfo.FullName);
}
}catch(Exception e){
Log.Error (TAG,e.ToString());
}
base.OnListItemClick(l, v, position, id);
}
public void RefreshFilesList(string directory)
{
//GenericExtFilter filter = new GenericExtFilter(extension);
IList<FileSystemInfo> visibleThings = new List<FileSystemInfo>();
var dir = new DirectoryInfo(directory);
try
{
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
visibleThings.Add(item);
}
}
catch (Exception ex)
{
Log.Error("FileListFragment", "Couldn't access the directory " + _directory.FullName + "; " + ex);
Toast.MakeText(Activity, "Problem retrieving contents of " + directory, ToastLength.Long).Show();
return;
}
_directory = dir;
_adapter.AddDirectoryContents(visibleThings);
// If we don't do this, then the ListView will not update itself when the data set
// in the adapter changes. It will appear to the user that nothing has happened.
ListView.RefreshDrawableState();
Log.Verbose("FileListFragment", "Displaying the contents of directory {0}.", directory);
}
如果我理解正确的话,您想创建 IFilenameFilter 来过滤 XML 个文件。
public class XmlFileFilter : Java.Lang.Object, IFilenameFilter
{
public bool Accept(File dir, string filename)
{
return filename.ToLower().EndsWith(".xml");
}
}
我用更简单的方法解决了这个问题 solution.I 在 all.Inside 上面的 RefreshFilesList 方法之后没有使用 IFileNameFilter,我做了以下更改:
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
if(item.IsDirectory())
{
visibleThings.Add(item);
}else if(item.IsFile())
{
bool isXmlFile = item.Extension.ToLower().EndsWith("xml");
if(isXmlFile)
{
visibleThings.Add(item);
}
}
}
现在显示所有文件夹和任何 xml 文件。