我需要做什么才能使此代码在可移植类库中工作? [英] What need I do to get this code to work in a Portable Class Library?

查看:57
本文介绍了我需要做什么才能使此代码在可移植类库中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道可移植类库的功能是否比紧凑框架受到更多的限制.

I'm wondering if the Portable Class Library is even more restricted in functionality than the Compact Framework.

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

I'm trying to port a CF/Windows CE app (runs on a handheld device) to a Xamarin solution that will target Android, iOS, Windows Phone, and perhaps other things.

不过,我遇到的问题之一是该旧代码(可在CF下工作):

One of the problems I run into, though, is that this legacy code (which works under 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解决方案中编译.我得到当前上下文中不存在名称'Directory'",然后右键单击该单词不能提供解析"选项.

...won't compile in the Xamarin/CPL solution. I get, "The name 'Directory' does not exist in the current context" and right-clicking that word does not afford a "resolve" option.

是否可以使PCL识别目录",还是必须完全重写代码?如果是后者,那么有人会建议/代替使用什么吗?

Is there a way to get PCL to recognize "Directory" or must I completely rewrite the code? If the latter, does anybody have any suggestions on what to do/use in its stead?

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

Relatedly, is there an URL that will show me what is [not] available in PCL and/or a site that will show how much of a provided block of code is "PCL-ready"?

本文中的第一张图片非常有启发性.稍后,它专门讨论PCL方案中不存在目录".

The first image in this article is very illuminating. Later on, it specifically talks about "Directory" not being available in the PCL scenario.

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

I downloaded the PCLStorage package referenced by Daniel Plaisted below to allow me to access the file system within a PCL project.

以下载页面[http://pclstorage.codeplex.com/]开头的示例代码为起点,到目前为止,我已经了解了以下内容:

Using the sample code at the start of the download page [http://pclstorage.codeplex.com/] as a starting point, I've gotten this far:

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()的arg是不正确的.我明白了,"参数1:无法从'string'转换为'System.Threading.CancellationToken'"

...but "EXTENSION" as the arg to GetFilesAsync() is not right. I get with this, "Argument 1: cannot convert from 'string' to 'System.Threading.CancellationToken'"

那我需要怎么做才能将所有* .XML文件都保存到文件夹中?

So what need I do to get all the *.XML files the folder?

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

This compiles, but I'm not at all sure it's the right way to do it, besides the fact that it simply gets all the files from the folder, rather than just those that match "*.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();
}

推荐答案

Windows Phone应用程序不使用文件系统进行操作 系统,并且只能使用隔离存储来持久化和 访问文件,因此该名称空间不提供任何其他功能 功能.

Windows Phone applications do not use the file system of the operating system and are restricted to using isolated storage to persist and access files, so this namespace does not provide any additional functionality.

http: //msdn.microsoft.com/zh-CN/library/windowsphone/develop/system.io%28v=vs.105%29.aspx

这篇关于我需要做什么才能使此代码在可移植类库中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆