Berit Larsen February 2016

OpenFileOutput does not exist in the current context

I have two files trying to do the same thing. The first is an activity, and it works:

[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.Stri        

Answers


Jason February 2016

OpenFileOutput is a method on the Context - in order for it to work in a generic class you need to pass in the current context, or retrieve it using Android.App.Application.Context

Post Status

Asked in February 2016
Viewed 2,119 times
Voted 4
Answered 1 times

Search




Leave an answer