[英]Using SQLite database in Xamarin.Forms
我已经创建了一个SQLite数据库(使用cmd shell sqlite3.exe),现在正尝试使用此页面在Xamarin.Forms中实现它https://developer.xamarin.com/guides/android/application_fundamentals/data/part_3_using_sqlite_orm/指导。 不过,我很困惑,因为我不知道数据库的路径是什么。 我将数据库命名为“ app”,表命名为“ tbl1”,但这就是我所知道的所有信息。 该数据库仅存储用户名和密码,并且仅在我的跨平台应用程序的登录页面上使用。
使用Xamarin.Forms
(您的链接适用于Android!),您将必须从共享项目中使用某种抽象来访问数据库(特定于平台)。 这就是您基本上可以在每个平台上获取正确的存储路径的方式。
这基本上意味着在共享项目中创建一个接口,即:
public interface IFileHelper
{
string GetLocalFilePath(string filename);
}
然后,您必须实现平台特定的部分。 即Android:
[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
。 更多信息在这里 。
这是有关本地数据库的官方文档 。
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.