我正在尝试创建一个任何人都可以访问的文件夹。当我尝试获取外部存储目录时
var path= Android.OS.Environment.ExternalStorageDirectory.ToString();
我看到错误:
Android does not contain a definition for 'OS'.
谁能帮我?
谢谢。
看起来问题出在我的项目类型上。我的项目是 Android XAML 应用程序(Xamarin Forms)。命名空间 Android.OS 仅在项目 Android App (Xamarin) 中可用。有办法解决吗?
您可以创建一个dependency service
以在表单上获取它。
Xamarin.Forms DependencyService 介绍:
创建接口:
public interface IDpendencyService
{
string GetExternalStorage();
}
Android 项目上的实现可能是这样的:
public class DependencyImplementation : IDpendencyService
{
public string GetExternalStorage()
{
var path = Android.OS.Environment.ExternalStorageDirectory.ToString();
return path;
}
}
不要忘记在 MainActivity.cs 中注册:
Xamarin.Forms.DependencyService.Register<IDpendencyService, DependencyImplementation>();
Xamarin.forms 中的用法:
private void Button_Clicked(object sender, EventArgs e)
{
var path = DependencyService.Get<IDpendencyService>().GetExternalStorage();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句