Raghuveer
Raghuveer

Reputation: 2637

Windows Phone Custom Live Tiles not working as Expected

I created a Windows Phone application, In that i need to create the custom tile and update it from program

I created a User control,

 <TextBlock x:Name="Count" Foreground="White" FontFamily="Segoe WP Bold" FontSize="80" Text="{Binding Count}" HorizontalAlignment="Right" Margin="0,0,10,0"/>
 <TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0,0,0,0" Grid.Row="1" Text="Tile Demo" Foreground="White" FontFamily="Segoe WP Semibold" FontSize="35" />

from my MainPage.xaml.cs file i am binding the count then i am converting the User control into a .jpg file and storing in the ISO Store. Like below

 public void CreateOrUpdateTile(int count) 
 {
 CustomNotificationTile frontTile = new CustomNotificationTile();
 TileData tileData = new TileData() { Count = count };
 frontTile.DataContext = tileData;

 //frontTile.Count.Text = count.ToString();

 frontTile.Measure(new Size(173, 173));
 frontTile.Arrange(new Rect(0, 0, 173, 173));
 var bmp = new WriteableBitmap(173, 173);
 bmp.Render(frontTile, null);
 bmp.Invalidate();

 var isf = IsolatedStorageFile.GetUserStoreForApplication();
 var filename = "/Shared/ShellContent/Tile.jpg";

 if (!isf.DirectoryExists("/Shared/ShellContent"))
 {
        isf.CreateDirectory("/Shared/ShellContent");
 }

 using (var stream = isf.OpenFile(filename, System.IO.FileMode.OpenOrCreate))
 {
 bmp.SaveJpeg(stream, 173, 173, 0, 100);
 }


 ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileID=2"));

 //test if Tile was created
 if (TileToFind == null)
 {
       StandardTileData NewTileData = new StandardTileData
       {
             BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute),
             BackBackgroundImage = new Uri("Application_TileImage_173x173.png", UriKind.Relative)
       };

       ShellTile.Create(new Uri("/MainPage.xaml?TileID=2", UriKind.Relative), NewTileData);
       }

 #region Update the Tile Not Working as expected

        else
        {
            StandardTileData NewTileData = new StandardTileData
            {
                BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute)
            };
            TileToFind.Update(NewTileData);
        }

        #endregion
  }

Problem:

My problem is first i am calling from constructor to create the tile with some dummy data, Its Working as expected

But when i try to update the Tile from other methods its not working, those methods i am calling from page_load event

Those methods will fetch data from a WCF service, i am updating the Tile from the completed event of the WCF service method like below

Service.getProductsCountAsync();

Service.getProductsCountCompleted += (o,e) => { 
int count = e.Result;
Dispatcher.BeginInvoke(() =>
        {
            CreateOrUpdateTile(count);
        });
};

When the controller hit the above code, I only seeing the number with black background, Tile count is updating but the background color and logo are changing, I don't know why its happening but need to solve as early as possible.

I think the problem might be updating the UI from the background thread, but i don't know how to overcome that

Following are the images which will taken before method calls and after method calls

Before

Before Updating

After

After service call

Upvotes: 2

Views: 780

Answers (1)

Raghuveer
Raghuveer

Reputation: 2637

Its working

We have to Bind the Background color, Logo also dynamically like count even though they are static

After that it is working as expected

Upvotes: 1

Related Questions