how do you create buttons dynamically in c# windows phone one per file name? -


i have app make folder on sdcard called "rpgapp" when ever creates character saves character html file in folder (all of works, have sdcard access in manifest , have .html added file associations) .this important information out sdcard access or file association unauthorized access exception.

i want create button per file in stack panel, here code im using

private async task readfiles()     {         z.test.clear();         storagefolder externaldevices = windows.storage.knownfolders.removabledevices;         storagefolder folder = (await externaldevices.getfolderasync("rpgapp"));         ireadonlylist<storagefile> filelist = await folder.getfilesasync();         foreach (storagefile file in filelist)         {             z.test.add(file.name);         }     }       public async void buttontest()     {         await readfiles();         foreach (string name in z.test)         {             button button1 = new button();             button1.height = 75;             button1.content = name;             button1.name = name;             teststackpanal.children.add(button1);         }     } 

there no errors far ide concerned. when try run buttontest() 2 systen.unauthorizedaccessexceptions error got before when creating files (fixed adding .html file associations) there no other files in rpgapp folder, forename-surname.html

i have been working @ of day , im stuck ideas creatly appreciated

windows phone universal app (windows phone 8.1 , above)

edit commenting out await readfiles(); , adding strings z.test work linked reading of files, know app has access folder , html files can create folder , files.

edit2 throw checking each line, line thats causing error

storagefolder folder = (await externaldevices.getfolderasync("rpgapp")); 

in readfiles method, , yet folder name

in methods calling asynchronous tasks execute on background thread. exception systen.unauthorizedaccessexceptions exception being thrown when trying access objects created on ui thread background thread.

i modify code after have finished reading files subsequent actions completed on ui thread. such (note not tested).

public async void buttontest() {     await readfiles();     deployment.current.dispatcher.begininvoke(() =>     {         foreach (string name in z.test)         {             button button1 = new button();             button1.height = 75;             button1.content = name;             button1.name = name;             teststackpanal.children.add(button1);         }     }); } 

the method deployment.current.dispatcher.begininvoke() { ... }); designed execute method manipulate ui. ui can manipulated ui thread.

the code above windows phone 8 8.1 has replaced few methods , access dispatcher wp8 use.

public async void buttontest() {     await readfiles();     coredispatcher dispatcher = corewindow.getforcurrentthread().dispatcher;     await dispatcher.runasync(coredispatcherpriority.normal, () =>     {         foreach (string name in z.test)         {             button button1 = new button();             button1.height = 75;             button1.content = name;             button1.name = name;             teststackpanal.children.add(button1);         }     }); } 

edit

ok.. have created own test enviroment figure out , not on right track. turns out problem reading sd card files correctly. done on simulator these steps.

1) open package manifest , got declarations tab. here created new filetype association (as pictured below) enter image description here

2) goto thecapabilities tab , ensure have removable storage selected (so can read storage).

now next part worked when changed way read files device. instead of trying load 1 folder iterate through folders, got same access denied error when trying query specific folder (not sure why). modified code read files iterate through folders , files adding ones required.

async task readallfiles(storagefolder folder) {     var files =await folder.getfilesasync();     foreach (var file in files)     {         test.add(file.name);     }      var folders = await folder.getfoldersasync();     foreach (var child in folders)         await readallfiles(child); }   public async void buttontest() {     test.clear();     storagefolder externaldevices = windows.storage.knownfolders.removabledevices;     var folders = await externaldevices.getfoldersasync();     foreach (var folder in folders)     {         await readallfiles(folder);     }     coredispatcher dispatcher = corewindow.getforcurrentthread().dispatcher;     await dispatcher.runasync(coredispatcherpriority.normal, () =>     {         foreach (string name in test)         {             button button1 = new button();             button1.height = 75;             button1.content = name;             button1.name = name;              teststackpanal.children.add(button1);         }     }); } 

you notice readallfiles method recursive calling parent folder. created simple list<string> test file names. seemed work.

now if wanted read rpgapp folder modify code following.. notice return false if folder null or folder name rpgapp. change more if rpgapp folder in root directory of removable storage.

async task<bool> continuetoreadallfiles(storagefolder folder) {     if (folder == null)         return false;     if (folder.name.equals("rpgapp", stringcomparison.currentcultureignorecase))     {         var files = await folder.getfilesasync();         foreach (var file in files)         {             test.add(file.name);         }         return false;     }     var folders = await folder.getfoldersasync();     foreach (var child in folders)         if (!await continuetoreadallfiles(child))             return false;      return true; }   public async void buttontest() {     test.clear();     storagefolder externaldevices = windows.storage.knownfolders.removabledevices;     var folders = await externaldevices.getfoldersasync();     foreach (var folder in folders)     {         if (!await continuetoreadallfiles(folder))             break;     }     //.... commented out } 

Popular posts from this blog