Windows UWP C# code works on desktop, not on mobile -


i'm trying change background wallpaper of desktop , windows mobile c#. works on desktop, not in windows mobile. have button click event executes changebackground:

private async void changebackgroundbutton_click(object sender, routedeventargs e) {     await changebackground();     updatetask(); }  private static async task changebackground() {     if (userprofilepersonalizationsettings.issupported())     {         storagefile file = task.run(async () => {             uri uri = new uri("https://source.unsplash.com/random/1080x1920");             storagefile f = await storagefile.createstreamedfilefromuriasync("background.jpg", uri, randomaccessstreamreference.createfromuri(uri));             return await f.copyasync(applicationdata.current.localfolder, "background.jpg", namecollisionoption.replaceexisting);             }).result;         userprofilepersonalizationsettings settings = userprofilepersonalizationsettings.current;         await settings.trysetwallpaperimageasync(file);     } } 

when press button on windows mobile, app gets stuck. button stays in hovered state, , wallpaper doesn't change.

what doing wrong?

edit: rewrote code fix problems copyasync. code looks now:

    private static async task<storagefile> changebackground()     {         if (userprofilepersonalizationsettings.issupported())         {             uri uri = new uri("https://source.unsplash.com/random/1920x1080");             string filename = datetime.now.tostring("yyyymmddhhmmssfff") + ".jpg";              httpclient httpclient = new httpclient();             httprequestmessage request = new httprequestmessage(httpmethod.get, uri);             httpresponsemessage response = await httpclient.sendasync(request, httpcompletionoption.responseheadersread);              var imagefile = await applicationdata.current.localfolder.createfileasync(filename, creationcollisionoption.replaceexisting);             var fs = await imagefile.openasync(fileaccessmode.readwrite);             datawriter writer = new datawriter(fs.getoutputstreamat(0));             writer.writebytes(await response.content.readasbytearrayasync());             await writer.storeasync();             writer.detachstream();             await fs.flushasync();              storagefile file = await applicationdata.current.localfolder.getfileasync(filename);              userprofilepersonalizationsettings settings = userprofilepersonalizationsettings.current;             if (!await settings.trysetwallpaperimageasync(file))             {                 debug.writeline("failed");             } else             {                 debug.writeline("success");             }             return file;         }         return null;     } 

on windows 10 shows success, on windows 10 mobile shows failed.

as said yesterday, can't figure out why copyasync() method stuck in first code when run on mobile. right use http download picture, there problems in second code, can't work on pc side.

it's clear can't use httpclient.sendasync() data form uri. here code:

private static async task changebackground() {     if (userprofilepersonalizationsettings.issupported())     {         uri uri = new uri("https://source.unsplash.com/random/1920x1080");         using (httpclient client = new httpclient())         {             try             {                 httpresponsemessage response = await client.getasync(uri);                 if (response != null && response.statuscode == httpstatuscode.ok)                 {                     string filename = "background.jpg";                     var imagefile = await applicationdata.current.localfolder.createfileasync(filename, creationcollisionoption.replaceexisting);                     using (irandomaccessstream stream = await imagefile.openasync(fileaccessmode.readwrite))                     {                         await response.content.writetostreamasync(stream);                     }                     storagefile file = await applicationdata.current.localfolder.getfileasync(filename);                     userprofilepersonalizationsettings settings = userprofilepersonalizationsettings.current;                     if (!await settings.trysetwallpaperimageasync(file))                     {                         debug.writeline("failed");                     }                     else                     {                         debug.writeline("success");                     }                 }             }             catch             {             }         }     } } 

by way, i'm using windows.web.http api, not system.net.http api.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -