在Xamarin.Forms中没有提供统一的方法。您可以利用DependencyService从您的共享代码中访问它,并仍然根据平台进行区分。这可能是这样的,定义一个接口在你的共享代码:
public interface IFilesystemService
{
string GetAppRootFolder();
}
现在创建这样在Android上实现:
public class FilesystemServiceAndroid : IFilesystemService
{
public string GetAppRootFolder()
{
return System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
}
}
对于iOS的想法是一样的,只有实施可能会有所不同。当然,您可以根据需要扩展此类,例如可以读取或写入文件。
不要忘记用[assembly: Xamarin.Forms.Dependency (typeof (FilesystemServiceAndroid))]
属性来装饰您的实现名称空间。像这样:
[assembly: Xamarin.Forms.Dependency (typeof (FilesystemServiceAndroid))]
namespace YourAppName
{
public class FilesystemServiceAndroid : IFilesystemService
{
// ... code here
}
}
现在你可以在你这样的共享代码检索:var path = DependencyService.Get<IFilesystemService>().GetAppRootFolder();
[装配:Xamarin.Forms.Dependency(typeof运算(FilesystemServiceAndroid))] 我在哪里有把这个在?我对此很陌生,对不起。我仍然得到空回 –
我已经更新了答案 –
我似乎仍然有错误。但是这次是用SQLiteConnection。你认为你能帮助我吗? :/ 我已经屏蔽了这个错误:http://puu.sh/va49n/f4431e525c.png –