WP7.8 ScheduledTaskAgent + RestSharp + Writeablebitmap problem

Iacopo Pace

New member
Mar 10, 2013
10
0
0
Visit site
Hi everyone,
I have a very strange problem. I am using a scheduled task to update my live tile, which is rendered using the TCD Controls live tile library through a JSON data retrieved from a REST API.
The tile is updated normally on WP8 (lumia 620) and WP7.8 when connected to the debugger (Lumia 800).

When WP7.8 is not connected to the debugger, the REST API is called, data retrieved but the tile won't be rendered... By debugging the app through toasts, I noticed the fault is inside the RenderTileTemplate method, which works good without the REST call, though (e.g.: getting the data locally, not through REST call).
Can anyone help me? Here is the code:

Code:
            Uri back = null;
            String txt = null;
            int fontSize = 0;
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                return;
            }
            var client = new RestClient("REST API URL");
            RestRequest reqCredit = new RestRequest();
            reqCredit.Resource = "allInfo";
            reqCredit.RootElement = "Values";
            reqCredit.AddParameter("uid", uid);
            reqCredit.AddParameter("pwd", pwd);
            reqCredit.RequestFormat = DataFormat.Json;
            reqCredit.Method = Method.GET;

            client.ExecuteAsync<AllInfoJSON>(reqCredit, (response)
                =>
            {
                if (response != null && response.StatusCode == HttpStatusCode.OK && !response.Data.status.Equals("error", StringComparison.OrdinalIgnoreCase))
                {                    
                    txt = response.Data.creditValues[0].value;
                    
                    fontSize = 0;
                    
                    Deployment.Current.Dispatcher.BeginInvoke(new Action(delegate//rendering done on the UI thread!!!!!
                    {
                        //MediumTileTemplate is custom but works... changing it to default one doesn't help....
                        back = TileRenderer.RenderTileTemplate(new MediumTileTemplate(txt, fontSize), "creditoTreTile", TileSize.Medium_336x336);//render the tile
                    }));
                }
            });            
            
            while (back == null)
            {
                System.Threading.Thread.Sleep(1000);
            }
            // Code is never reached unless debugging on WP7.8 or WP8
            String backTitle = "Il " + DateTime.Now.Date.Day + "/" + DateTime.Now.Date.Month + "@" + DateTime.Now.ToString("HH:mm");
            if (TileSetter.IsNewTilesSupported) {
                if (DBManagement.isInfoFrontOn())
                {
                    TileSetter.SetPrimaryTileFlipTemplate(backTitle, "Credito Tre", "", "", 0, null, back, new Uri("Background.png", UriKind.Relative), null, null);
                } else {
                    TileSetter.SetPrimaryTileFlipTemplate("Credito Tre", backTitle, "", "", 0, null, new Uri("Background.png", UriKind.Relative), back, null, null);
                }
            } else {
                if (DBManagement.isInfoFrontOn())
                {
                    TileSetter.SetPrimaryTile(backTitle, "Credito Tre", "", 0, back, new Uri("Background.png", UriKind.Relative));
                } else {
                    TileSetter.SetPrimaryTile("Credito Tre", backTitle, "", 0, new Uri("Background.png", UriKind.Relative), back);
                }
            }
            
            NotifyComplete();

And the RenderTileTemplate code, is public on NuGet, and reported here. The error is at the Render call or, time to time, at the Encode call:
Code:
public static Uri RenderTileTemplate(Control template, string filename, TileSize tileSize = TileSize.Medium_336x336)
        {
            string dims = tileSize.ToString();
            dims = dims.Substring(dims.LastIndexOf('_') + 1);
            int[] size = new int[] { Convert.ToInt32(dims.Substring(0, 3)), Convert.ToInt32(dims.Substring(4, 3)) };
            template.Measure(new Size(size[0], size[1]));
            template.Arrange(new Rect(0, 0, size[0], size[1]));
            WriteableBitmap wb = new WriteableBitmap(size[0], size[1]);
            wb.Render(template, null);
            wb.Invalidate();
            var encoder = new PngEncoder();
            using (var str = new IsolatedStorageFileStream(string.Format(pattern, filename), FileMode.Create, isoStore))
            {
                encoder.Encode(wb.ToImage(), str);
            }
            return //The URI PATH;
        }

Thanks a lot! :)
 

Members online

Forum statistics

Threads
323,187
Messages
2,243,412
Members
428,037
Latest member
Brilliantick99