Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Context.GetExternalFilesDir()不接受null_C#_Xamarin.android_Android External Storage - Fatal编程技术网

C# Context.GetExternalFilesDir()不接受null

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

我正在尝试获取我的c#xamarin android应用程序的私有外部存储目录的路径。microsoft文档告诉我们使用

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"); 
    }
}