使用Xamarin.Forms(您的链接适用于Android!),您必须使用共享项目中的某种抽象来访问您的数据库(这是特定于平台的)。这就是您基本上可以获得每个平台的正确存储路径的方法。
这基本上意味着在共享项目中创建一个接口,即:
public interface IFileHelper
{
string GetLocalFilePath(string filename);
}
然后,您必须实现平台特定的部分。即安卓:
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.Droid
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(path, filename);
}
}
}
iOS:
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.iOS
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libFolder = Path.Combine(docFolder, "..", "Library", "Databases");
if (!Directory.Exists(libFolder))
{
Directory.CreateDirectory(libFolder);
}
return Path.Combine(libFolder, filename);
}
}
}
UWP:
using Windows.Storage;
...
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.UWP
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
}
}
}
然后去检索和使用 最简单的方法是使用Xamarin的DependencyService。更多信息here。
这里是the official documentation about local databases。
希望对你有帮助!