OpenFileOutput 在当前上下文中不存在

本文关键字:不存在 上下文 OpenFileOutput | 更新日期: 2023-09-27 18:33:53

我有两个文件试图做同样的事情。第一个是活动,它有效:

[Activity (Label = "Local Files sample", MainLauncher = false)]
public class Activity1 : Activity
{
    int count = 0;
    static readonly string Filename = "count";
    string path;
    string filename;

    protected override async void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main2);
        path = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
        filename = Path.Combine (path, Filename);
        Task<int> loadCount = loadFileAsync ();
        Console.WriteLine ("Could be excueted before load finished!");
        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
        Button btnSave = FindViewById<Button> (Resource.Id.btnSave);
        Button btnReset = FindViewById<Button> (Resource.Id.btnReset);
        TextView txtStored = FindViewById<TextView> (Resource.Id.stored);
        TextView txtPath = FindViewById<TextView> (Resource.Id.path);
        button.Click += delegate {
            button.Text = string.Format ("{0} clicks!", ++count); };
        btnSave.Click += async delegate {
            btnSave.Text = string.Format ("Current count saved: {0}", count);
            txtStored.Text = string.Format (this.GetString (Resource.String.stored), count);
            await writeFileAsync();
        };
        btnReset.Click += delegate {
            File.Delete (filename);
            btnSave.Text = this.GetString (Resource.String.save);
            txtStored.Text = string.Format (this.GetString (Resource.String.stored), 0);
        };
        count = await loadCount;
        txtPath.Text = filename;
        txtStored.Text = string.Format (this.GetString (Resource.String.stored), count);
    }
    async Task<int> loadFileAsync()
    {
        if (File.Exists (filename)) {
            using (var f = new StreamReader (OpenFileInput (Filename))) {
                string line;
                do {
                    line =await f.ReadLineAsync();
                } while (!f.EndOfStream);
                Console.WriteLine ("Load Finished");
                Log.Info ("---", "Loaded=" + line);
                return int.Parse (line);
            }
        }
        return 0;
    }
    async Task writeFileAsync()
    {
        using (var f = new StreamWriter (OpenFileOutput (Filename, FileCreationMode.Append | FileCreationMode.WorldReadable))) {
            await f.WriteLineAsync (count.ToString ()).ConfigureAwait(false);
        }
        Console.WriteLine ("Save Finished!");
        Log.Info ("---", "Saved=" + count);
    }
}

第二个是 C# 类,它不起作用:

public class Class_IO
{
    int count = 0;
    //static readonly string Filename = "count";
    string path;
    string filename;
    public Class_IO ()
    {
    }
    async Task<int> loadFileAsync ()
    {
        if (File.Exists (filename)) {
            using (var f = new StreamReader (OpenFileInput (Filename))) {
                string line;
                do {
                    line = await f.ReadLineAsync ();
                } while (!f.EndOfStream);
                Console.WriteLine ("Load Finished");
                Log.Info ("---", "Loaded=" + line);
                return int.Parse (line);
            }
        }
        return 0;
    }
    async Task writeFileAsync ()
    {
        using (var f = new StreamWriter (OpenFileOutput (Filename, FileCreationMode.Append | FileCreationMode.WorldReadable))) {
            await f.WriteLineAsync (count.ToString ()).ConfigureAwait (false);
        }
        Console.WriteLine ("Save Finished!");
        Log.Info ("---", "Saved=" + count);
    }
}

为什么OpenFileInput在"当前上下文"中不可用?我如何让它工作?

OpenFileOutput 在当前上下文中不存在

OpenFileOutput 是上下文上的一种方法 - 为了让它在泛型类中工作,您需要在当前上下文中传递,或使用 Android.App.Application.Context 检索它