c# - 我需要做什么才能使此代码在可移植类库中工作?

标签 c# xamarin.android xamarin portable-class-library compact-framework2.0

我想知道可移植类库在功能上是否比 Compact Framework 更受限制。

我正在尝试将 CF/Windows CE 应用程序(在手持设备上运行)移植到 Xamarin 解决方案,该解决方案将针对 Android、iOS、Windows Phone 以及可能的其他应用程序。

不过,我遇到的一个问题是这个遗留代码(在 CF 下工作):

public static List<string> GetXMLFiles(string fileType, string startingDir)
{
    const string EXTENSION = ".XML";
    string dirName = startingDir;
    // call it like so: GetXMLFiles("ABC", "\\");  <= I think the double-whack is what I need for Windows CE device...am I right?
    var fileNames = new List<String>();
    try
    {
        foreach (string f in Directory.GetFiles(dirName))
        {
            string extension = Path.GetExtension(f);
            if (extension != null)
            {
                string ext = extension.ToUpper();
                string fileNameOnly = Path.GetFileNameWithoutExtension(f);
                if (fileNameOnly != null &&
                    ((ext.Equals(EXTENSION, StringComparison.OrdinalIgnoreCase)) &&
                     (fileNameOnly.Contains(fileType))))
                {
                    fileNames.Add(f);
                }
            }
        }
        foreach (string d in Directory.GetDirectories(dirName))
        {
            fileNames.AddRange(GetXMLFiles(fileType, d));
                // from Brad Rem's answer here: http://stackoverflow.com/questions/22186198/why-is-this-function-returning-nothing-although-there-is-a-match/22186351?noredirect=1#22186351
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return fileNames;
}

...不会在 Xamarin/CPL 解决方案中编译。我得到“名称‘目录’在当前上下文中不存在”并且右键单击该词不提供“解决”选项。

有没有办法让 PCL 识别“目录”,或者我必须完全重写代码?如果是后者,有人对代替它做什么/使用有什么建议吗?

相关地,是否有一个 URL 可以显示 PCL 中[不]可用的内容和/或一个网站可以显示提供的代码块中有多少是“PCL 就绪”的?

更新

this article 中的第一张图片很有启发性。后面专门讲了PCL场景下“Directory”不可用。

更新 2

我下载了下面 Daniel Plaisted 引用的 PCLStorage 包,以允许我访问 PCL 项目中的文件系统。

使用下载页面 [http://pclstorage.codeplex.com/] 开头的示例代码作为起点,我已经做到了这一点:

public async Task<List<string>> GetXMLFiles(string fileType, string startingDir)
{
    const string EXTENSION = ".XML";

    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.GetFolderAsync(startingDir, CreationCollisionOption.OpenIfExists); //CreateFolderAsync(startingDir, CreationCollisionOption.OpenIfExists);
    List<string> fileNames = await folder.GetFilesAsync(EXTENSION);
    return fileNames;
}

...但是“EXTENSION”作为 GetFilesAsync() 的参数是不正确的。我明白了,“参数 1:无法从‘string’转换为‘System.Threading.CancellationToken’

那么我需要做什么才能将所有 *.XML 文件放入文件夹?

更新 3

这可以编译,但我完全不确定这是正确的方法,除了它只是从文件夹中获取所有文件,而不仅仅是那些匹配“*.XML":

public async Task<List<IFile>> GetXMLFiles(string fileType, string startingDir)
{
    const string EXTENSION = ".XML";

    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.GetFolderAsync(startingDir, System.Threading.CancellationToken.None);
    IList<PCLStorage.IFile> fileNames = await folder.GetFilesAsync(System.Threading.CancellationToken.None);

    return fileNames.ToList();
}

最佳答案

因为在 PCL 中我无法从字符串中获取 StreamWriter(它需要一个流),所以我创建了一个简单的接口(interface)来从平台实现中获取一些数据。您也可以使用 DirectoryInfo 和 FileInfo 执行此操作。

https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Text/IStreamLocator.cs

实现也非常简单,只需要一个 WP8 编译器标志:

https://github.com/sami1971/SimplyMobile/blob/master/WP8/SimplyMobile.Text.Platform/StreamLocator.cs

递归搜索 *.XML 文件:

    private static void PrintDirectory(IStreamLocator locator, string dir)
    {
        foreach (var file in locator.GetFileNames(dir, "*.XML"))
        {
            System.Diagnostics.Debug.WriteLine(file);
        }

        foreach (var di in locator.GetFolderNames(dir, "*"))
        {
            PrintDirectory(locator, di);
        }
    }

关于c# - 我需要做什么才能使此代码在可移植类库中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615794/

相关文章:

c# - 检查用户名或用户电子邮件已存在

xamarin - java.lang.NoSuchMethodError : no non-static method "Landroid/app/Fragment;. getContext()

xamarin - 更新到 Visual Studio 2017 版本 15.8 预览版后,出现 "Can not resolve reference: ` System.Memory `"或 "Could not find ` System.Memory `"

c# - VS2017 和 VS2019 的主要 Activity 区别

c# - 我可以在 Xaml 中使用 Device.OnPlatform 设置设备特定 View 吗?

c# - 如何使用 Web 服务加载 HTML 文件,包括其在 Windows Phone 8 的 WebBrowser 中的 css 文件

c# - 请向我解释这个 BackgroundWorker 事件

c# - 从 Windows 客户端应用程序管理远程 *UNIX 服务器上的文件的方法

android - Xamarin - 找不到与给定名称匹配的资源(在 'theme' 处,值为 '@style/MyTheme')

c# - .Net CultureInfo Month Names 返回一个额外的空字符串