Xamarin文件保存问题

时间:2018-01-15 22:22:26

标签: c# xamarin xamarin.forms

我正在开发一个Xamarins.Forms跨平台社交媒体应用。

当用户登录时,我保存从数据库中抓取的会话。

当用户按下登录时,我运行以下行,page_result它是会话,我知道它不是null我已经检查过。

FileUtils.UpdateSession(page_result)

这是我的FileUtils类(不是我在教程中找到的工作)

public async static Task<IFile> CreateFile(this string filename, IFolder rootFolder = null)
    {
        IFolder folder = rootFolder ?? FileSystem.Current.LocalStorage;
        IFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
        return file;
    }
    public async static Task<bool> WriteTextAllAsync(this string filename, string content = "", IFolder rootFolder = null)
    {
        IFile file = await filename.CreateFile(rootFolder);
        await file.WriteAllTextAsync(content);
        return true;
    }

    public async static Task<string> ReadAllTextAsync(this string fileName, IFolder rootFolder = null)
    {
        string content = "";
        IFolder folder = rootFolder ?? FileSystem.Current.LocalStorage;
        bool exist = await fileName.IsFileExistAsync(folder);
        if (exist == true)
        {
            IFile file = await folder.GetFileAsync(fileName);
            content = await file.ReadAllTextAsync();
        }
        return content;
    }
    public async static Task<bool> DeleteFile(this string fileName, IFolder rootFolder = null)
    {
        IFolder folder = rootFolder ?? FileSystem.Current.LocalStorage;
        bool exist = await fileName.IsFileExistAsync(folder);
        if (exist == true)
        {
            IFile file = await folder.GetFileAsync(fileName);
            await file.DeleteAsync();
            return true;
        }
        return false;
    }

然后我添加了这个以增加对我需要的支持

public async static Task<bool> UpdateSession(this string sessionID)
    {
        await CreateFile("session");
        await WriteTextAllAsync("session", sessionID);
        return true;
    }

    public async static Task<string> GetSession()
    {
        IFolder folder = await GetFolder("session");
        return await ReadAllTextAsync("session", folder);
    }

    public async static Task LogoutSession()
    {
        IFolder folder = await GetFolder("session");
        await DeleteFile("session", folder);
    }

当我首先按下登录按钮时,它非常缓慢,大约需要2分钟才能完成任何操作,我不能花那么长时间才会出现我做错的事情吗?它也会在2分钟后崩溃并说堆栈跟踪打印到&#39; /data/anr/traces.txt'但是我无法找到它。

1 个答案:

答案 0 :(得分:0)

我通过使用MySql修复了问题。