【问题标题】:Port from winForms to Windows phone 8.0 C#从 winForms 移植到 Windows phone 8.0 C#
【发布时间】:2016-06-10 21:29:33
【问题描述】:

有人可以通过将 winForms 中编写的这段 c# 代码移植/翻译到 Windows phone 8.0 来帮助我吗?我真的很挣扎,我无法让它工作我可能阅读了我可以拿到的每一个文档,但我就是不能让它工作。我知道 stackover 流不是人们为你编写代码的地方,但我没有希望.. 任何帮助都值得赞赏.这是代码:

private static string[] _folderPaths = Directory.GetDirectories(@"Folders");
    private string[] _imagePaths;
    private List<string> _questionsPaths = new List<string>();
    private List<string> _correctAnswersPaths = new List<string>();
    private List<string> _allAnswersPaths = new List<string>();

for (int i = 0; i < _folderPaths.Length; i++)
        {
            var _tempLocations = Directory.GetFiles(_folderPaths[i], (i + 1).ToString() + "*.txt*");
            _imagePaths = Directory.GetFiles(_folderPaths[i], "*.png", SearchOption.TopDirectoryOnly);
            foreach (string item in _tempLocations)
            {
                if (item == "Folders\\" + (i + 1).ToString() + "\\" + (i + 1).ToString() + ".txt")
                {
                    _questionsPaths.Add(item);
                }
                if (item == "Folders\\" + (i + 1).ToString() + "\\" + (i + 1).ToString() + "answer" + ".txt")
                {
                    _correctAnswersPaths.Add(item);
                }
                if (item == "Folders\\" + (i + 1).ToString() + "\\" + (i + 1).ToString() + "answers" + ".txt")
                {
                    _allAnswersPaths.Add(item);
                }
            }
        }

【问题讨论】:

  • 那么什么不起作用?它怎么不工作?它哪里出错了?你所说的只是“我不能让它工作”,这是非常模糊的。你这是什么意思?
  • 我放弃了为 windows phone 做这件事只是因为我无法将这段 winForms 代码放入我的 windows phone 应用程序中。我没有留下任何样品来表示我真的很抱歉..

标签: c# windows winforms xaml windows-phone


【解决方案1】:

Windows Phones 中使用的对应类型是(当然不是精确映射):

Directory -> StorageFolder
Directory.GetDirectories -> StorageFolder.GetFoldersAsync
Directory.GetFiles -> StorageFolder.GetFilesAsync

所以翻译后的代码应该是这样的(你也需要熟悉新的await-async关键字)

using Windows.Storage; 

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Folders"); 
var subFolders = await folder.GetFoldersAsync();
foreach (StorageFolder subFolder in subFolders)
{
    var tempFiles = await subFolder.GetFilesAsync(...

【讨论】:

  • 他们现在在代码级别将目录重命名为文件夹,而不仅仅是在 UI 级别?哇。
  • ApplicationData.Current.LocalFolder 指的是哪里?我得到 FileNotFoundException
  • @MattiVirkkunen 我可以互换使用它们
  • 这是我的“文件夹”文件夹 Desktop\WPBluetoothDemo\WPBluetoothDemo\Bin\Debug 我如何访问它的当前位置?
  • 试试Package.Current.InstalledLocation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多