How to: Schedule Tile Updates for Windows Phone
March 22, 2012
This topic introduces you to the steps needed to update the background image of an Application Tile using ShellTileSchedule. ShellTileSchedule can also be used to update secondary Tiles. Tiles Overview for Windows Phone has information on Tile properties and the various methods that can be used to update Tiles.
Tip: |
|---|
The background image of the front of the Tile is the only property that can be updated using ShellTileSchedule. |
You can find this completed sample in Code Samples for Windows Phone.
ShellTileSchedule allows you to set up a one-time or recurring schedule that will update the background image of the Tile. The schedule will continue to run even if your application is not active. ShellTileSchedule can also be used to stop any schedule running for your application. Applications should store their settings for ShellTileSchedule and start the schedule each time the applications are started because failures in the schedule, even when the application is no longer running, can cancel out a schedule.
The ShellTileSchedule sample is a simple program with four buttons. The four buttons:
-
Update a Tile once.
-
Create a schedule to update a Tile indefinitely.
-
Create a schedule to update a Tile for a specific number of times.
-
Stop any schedule that is running.
Note:
|
|---|
|
Remember to pin an Application Tile for the application to Start in order to test the Tile update functionality. |
Create the applications UI
-
In Visual Studio 2010 Express for Windows Phone, create a new project by selecting the File | New Project menu command.
-
The New Project window is displayed. Expand the Visual C# templates, and then select the Silverlight for Windows Phone templates.
-
Select the Windows Phone Application template. Fill in the Name with a name of your choice.
-
Click OK. The New Windows Phone Application window is displayed.
-
In the Target Windows Phone OS Version menu, ensure that Windows Phone OS 7.1 is selected.
-
Click OK. A new project is created, and MainPage.xaml is opened in the Visual Studio designer window.
-
On MainPage.xaml, replace the Grid named ContentPanel with the following code. This will create the four buttons for our UI.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Start One Time Schedule" Height="72" HorizontalAlignment="Center" Margin="38,42,38,0" Name="buttonOneTime" VerticalAlignment="Top" Width="400" Click="buttonOneTime_Click" /> <Button Content="Start Indefinite Schedule" Height="72" HorizontalAlignment="Center" Margin="38,120,38,0" Name="buttonIndefinite" VerticalAlignment="Top" Width="400" Click="buttonIndefinite_Click" /> <Button Content="Start Defined Count Schedule" Height="72" HorizontalAlignment="Center" Margin="38,198,38,0" Name="buttonDefined" VerticalAlignment="Top" Width="400" Click="buttonDefined_Click"/> <Button Content="Stop Schedule" Height="72" HorizontalAlignment="Center" Margin="38,276,38,0" Name="buttonStop" VerticalAlignment="Top" Width="400" Click="buttonStop_Click" /> </Grid>
Create a Tile Schedule
-
Add a using directive to the top of the MainPage.xaml.cs file and provide the name of the namespace that includes ShellTileSchedule.
-
Declare and initialize your variables for the schedule and the status.
-
Add the code to do a one-time update.
private void buttonOneTime_Click(object sender, RoutedEventArgs e) { // Update will happen one time. SampleTileSchedule.Recurrence = UpdateRecurrence.Onetime; // Start the update schedule now, but because updates are batched, the update may not happen for an hour or so. SampleTileSchedule.StartTime = DateTime.Now; SampleTileSchedule.RemoteImageUri = new Uri(@"http://www.weather.gov/forecasts/graphical/images/conus/MaxT1_conus.png"); SampleTileSchedule.Start(); TileScheduleRunning = true; }
-
Add code to set up a schedule that will run indefinitely. Remember that if the schedule fails too many times, it will be canceled.
private void buttonIndefinite_Click(object sender, RoutedEventArgs e) { // Updates will happen on a fixed interval. SampleTileSchedule.Recurrence = UpdateRecurrence.Interval; // Updates will happen every hour. Because MaxUpdateCount is not set, the schedule will run indefinitely. SampleTileSchedule.Interval = UpdateInterval.EveryHour; SampleTileSchedule.RemoteImageUri = new Uri(@"http://www.weather.gov/forecasts/graphical/images/conus/MaxT1_conus.png"); SampleTileSchedule.Start(); TileScheduleRunning = true; }
-
Add code to set up a schedule that will update the tile a finite number of times. Again, if the schedule fails too many times, it will be cancelled.
private void buttonDefined_Click(object sender, RoutedEventArgs e) { // Updates will happen on a fixed interval. SampleTileSchedule.Recurrence = UpdateRecurrence.Interval; // Updates will happen every hour. SampleTileSchedule.Interval = UpdateInterval.EveryHour; // Do a maximum of 50 updates and then stop. SampleTileSchedule.MaxUpdateCount = 50; SampleTileSchedule.RemoteImageUri = new Uri(@"http://www.weather.gov/forecasts/graphical/images/conus/MaxT1_conus.png"); SampleTileSchedule.Start(); TileScheduleRunning = true; }
-
Add code to stop any running schedule. Note how we attach to a schedule by first starting it.
Debugging an update schedule can be challenging. To conserve power, the updates are batched, so it may take up to an hour or so to see an updated image. Updates will not occur when the device is locked since the user would not see the updates.
To run and debug your application
-
Run the application by selecting the Debug | Start Debugging menu command.
-
When the Emulator initializes and your program is running, press the Start button on the Emulator to go to the Start screen. Navigate over to the Applicaton List and find your application. Tap and hold the application name and then select pin to start from the context menu. The Application Tile is pinned to Start.
-
Press the Back button to return to your application. Press one of the buttons on the application to start a schedule.
-
Return to Start. Wait for the schedule to run to see the result. Remember that this may take a while since the updates are batched.
Tip: