繁体   English   中英

OpenFileOutput在当前上下文中不存在

[英]OpenFileOutput does not exist in the current context

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

[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是Context上的一种方法-为了使其能够在通用类中工作,您需要在当前上下文中传递或使用Android.App.Application.Context对其进行检索

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM