c# - OpenFileOutput does not exist in the current context -
i have 2 files trying same thing. first activity, , 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 "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 excueted before load finished!"); // our button layout resource, // , attach event 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; { 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); } }
the second c# class, , doesn't work:
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; { 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); } }
why openfileinput not available in "the current context"? how make work?
openfileoutput method on context - in order work in generic class need pass in current context, or retrieve using android.app.application.context
Comments
Post a Comment