WinRT Background Task Processes

I’ve been working on a Windows Phone 8.1 project which has several background tasks. One of these uses the device’s sensors – using a DeviceUseTrigger. This is different to how a regular periodic task works because the task implementation creates a deferral and keeps running handling the event generated by the sensor device until it is explicitly cancelled. The task is created normally with a class implementing IBackgroundTask and runs under the context of BackgroundTaskHost.exe. Because I wanted to share some data with the other tasks as a when they run I was interested if these other IBackgroundTask implementations were also hosted in the same process. If so this means that any static instances would be shared between them. Since I couldn’t find the answer I did some testing with some additional debugging and discovered that no, they each run in a separate process.

This lead me to explore how to share data between them. There are no built in IPC classes in the Windows Runtime like MessageQueues or similar so really the only options are the LocalSettings for individual setting values and files placed in the LocalFolder for any other data. The added complication here is that you have to handle the situation where both processes may try to read/write the file at the same time. Luckily you can use a named Mutex to enforce exclusive access to the file and have the other process wait on the Mutex.

Advertisements

Rating control for Windows Phone 8.1 Apps

In Silverlight 8.x you can use the Toolkit Rating control to provide standard star rating functionality in your app. If you’re moving to a Windows Phone 8.1 App project you can’t use the Silverlight based toolkit and there isn’t a native control to replace it.

If you’ve worked with Windows Store apps you’re probably familiar with the Callisto toolkit – a set of controls for Windows Store apps that brought things like settings flyouts (prior to them being rolled into the Windows Runtime) and a rating control. You can use this control within a Phone app with one caveat. If you try to reference the NuGet package for Callisto it will tell you it contains no library for Phone apps. However if you are using a Universal project this is easy to overcome:-

  1. Add the Callisto NuGet package to your Windows Store app
  2. In your Phone project select Add Reference and browse to the packages folder in the root of your solution
  3. Locate Callisto.1.4.0/lib/netcore451 and select Callisto.dll
  4. Bingo you can now add the rating control to your phone project.

I haven’t tested all the functionality in a Phone project so it is possible there may be some items which don’t work on Phone…

Hopefully in a future update to the Callisto NuGet package it will include a dll for Phone 8.1 apps too to avoid these extra steps…

 

Linking to your other apps

Using the Windows Phone SDK the only method to return a list of your other published apps (e.g. for your About page) is to use the MarketplaceSearchTask and specify your company name in the search criteria. The problem with this is that it doesn’t return the actual publisher list but does a keyword search so you may find that a few other apps appear if other devs have used your company name in their descriptions or keywords.

The solution is to use a Uri and the LaunchUriAsync method in Windows Phone 8. The zune (yes it’s a throwback from the olden days) Uri scheme includes the ability to specify a publisher name e.g.

Windows.System.Launcher.LaunchUriAsync(new Uri("zune:search?publisher=APPA Mundi Ltd"));

In case you don’t want to hard-code your publisher name (to create a shared component for example) you can add this from the metadata of your app using Charming ApplicationModel e.g.

Windows.System.Launcher.LaunchUriAsync(new Uri("zune:search?publisher=" + InTheHand.ApplicationModel.Package.Current.PublisherDisplayName));

The equivalent functionality in Windows 8 is implemented through the ms-windows-store Uri scheme:-

ms-windows-store:Publisher?name=APPA%20Mundi%20Ltd

or again using package information:-

Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:Publisher?name=" + Windows.ApplicationModel.Package.Current.PublisherDisplayName));

Launching System Features From Windows Store Apps

In Windows Phone we have a number of Launchers and Choosers for interacting with system features. For the most part the approach used in Windows Store is to use special URI schemes to achieve the same end result and launch platform features. I’ve collated together a list below of some known ones. Some like maps are documented in detail, others not. All can be launched with Windows.System.Launcher.LaunchUriAsync() (which also exists in Windows Phone 8 for launching other apps via a uri).

 

ms-windows-store: – Windows Store “Hub”

ms-windows-store:PDP?PFN={your package family name} – App detail page. Get your PFN from Windows.ApplicationModel.Package.Current.Id.FamilyName

ms-windows-store:REVIEW?PFN={your package family name} – Write a review page

ms-windows-store:Search?query={query term} -Remember to URI escape your query term text

 

xboxgames: – launches the games hub

 

The following launch Microsoft/Bing apps (if installed)

bingfinance: – Finance

bingfoodanddrink: – Food & Drink

binghealthnfitness: – Health & Fitness

bingmaps: – Maps. Full details here:- http://msdn.microsoft.com/en-us/library/windows/apps/jj635237.aspx

bingnews: – News

bingsports: – Sports

bingtravel: – Travel

bingweather: – Weather

microsoftmusic: – Music

microsoftvideo: – Video

ms-mail: – Mail

windowsreadinglist: – Reading List

wlcalendar: – Calendar

wlpeople: – People

xboxsmartglass: – Smart Glass

 

Jumping back to the first set which cover the Store. In order to make these simpler to call and help with porting existing code from Windows Phone I’ve added a new library to the Charming suite. This is the first library to go the other way – provide a phone style API for Windows Store.

https://www.nuget.org/packages/InTheHand.Phone.Tasks.RT/

This library provides the MarketplaceHubTask, MarketplaceDetailTask and MarketplaceSearchTask which we know and love from Windows Phone and launches the equivalent functionality on Windows. The key differences beside the namespaces are that the content identifier for the MarketplaceDetailTask is a Package Family Name and there is no ContentType property to distinguish between apps, games and music. For the purposes of Windows Store all apps and games are treated the same and there is no music in the Windows Store. Code and documentation will be updated on the CodePlex site shortly though if you’ve used these features under Windows Phone you already know how to use it!

Detect When Running on Windows Simulator

The Windows Simulator is a deployment target for Visual Studio which allows you to run your Windows Store apps in a simulated device. This allows you to test touch interaction and different screen sizes and orientations on your development machine. The way this works is that it creates a Remote Desktop session into your PC. This is why when you run it you’ll see your own start screen and user account. The problem with this is that some APIs fail on the simulator such as setting up a Push Notification channel. We need a way to detect if we are running in the simulator so that we can react accordingly. If you try to poke around for hardware information you’ll find it returns the same as running directly on your machine. This makes sense when you know it’s a Remote Desktop session. Luckily there is an API specifically designed for this scenario:-


bool isRemote = Windows.System.RemoteDesktop.InteractiveSession.IsRemote;

This will return true in a Simulator session. We can use this to avoid calling any code which will fail on the simulator and can easily switch between local machine and simulator deployment to test various aspects of the app without touching the code or using different project configurations and conditional compilation.