C# Context.GetExternalFilesDir()不接受null
我正在尝试获取我的c#xamarin android应用程序的私有外部存储目录的路径。microsoft文档告诉我们使用C# Context.GetExternalFilesDir()不接受null,c#,xamarin.android,android-external-storage,C#,Xamarin.android,Android External Storage,我正在尝试获取我的c#xamarin android应用程序的私有外部存储目录的路径。microsoft文档告诉我们使用 Android.Content.Context.GetExternalFilesDir(string) 但是,当我尝试使用此函数时,我得到与该函数相关的以下错误: An object reference is required for the non-static field, method or property 这是我的密码 class RecorderControl
Android.Content.Context.GetExternalFilesDir(string)
但是,当我尝试使用此函数时,我得到与该函数相关的以下错误:
An object reference is required for the non-static field, method or property
这是我的密码
class RecorderController {
string externalStoragePath;
public RecorderController() {
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
// I have also tried the following:
// string s = null;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
// Even when I try to get the path to the Downloads folder, I get the same error:
// string s = Android.OS.Environment.DirectoryDownloads;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
}
}
我不知道如何解决这个问题,有人知道我做错了什么吗?尝试改变
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
到
显然,我必须使用一个上下文实例,就像Mike Christensen评论的那样。我还忽略了GetExternalFilesDir返回一个File对象,我应该在它之后使用AbsolutePath,就像Leo Zhu说的那样 我把代码改成了
class RecorderController {
string externalStoragePath;
public RecorderController(Context con) {
PermanenteOpslagPad = Path.Combine(con.GetExternalFilesDir(null).AbsolutePath, "recordings");
}
}
看起来
GetExternalFilesDir
是一个实例方法,Context
是一个类?
class RecorderController {
string externalStoragePath;
public RecorderController(Context con) {
PermanenteOpslagPad = Path.Combine(con.GetExternalFilesDir(null).AbsolutePath, "recordings");
}
}