Category: Windows Phone

Build Windows Phone Apps Without Any Code

Microsoft have today announced Windows Phone App Studio Beta (https://apps.windowsstore.com/default.htm). This is a web based tool which allows you to build your own app by putting together data sources and customising the look and feel. The instructions list just four basic steps and there is a library of templates to start from. This might not be of huge interest to hardcore developers but it does allow non-developers to build and test apps and get a feel for what the platform is capable of. One of the interesting features is that once you’ve created your app you can download the source code as a Visual Studio solution and then customise it however you see fit.

To investigate this I picked a couple of the Charming libraries (http://charming.codeplex.com) to add Search and Sharing functionality to a ready-made app. I’ll be following up with a detailed step-by-step approach of adding Search support to a generated app.

Charming ApplicationModel

My previous couple of posts have been related to the Sharing functionality provided by the Charming libraries. This one looks at a smaller (but no less exciting) component. For Windows Phone development if you want to retrieve the app information at runtime you have to read the WMAppManifest.xml file included in the package. In Windows 8 an API was provided (Windows.ApplicationModel.Package and related classes) to provide a strongly typed API around the Windows Store metadata. Although these classes are present in Windows Phone projects they aren’t all implemented and you can waste a lot of time getting exceptions assuming that the functionality will work.

Enter Charming ApplicationModel. This replicates the same object model you see for Windows Store apps but in a way which actually works and reads information from the xap. In addition to this there is an added property – Capabilities which returns the combination of capabilities requested by your package. This has been especially useful in developing the other Charming libraries as it makes it possible to add functionality only when certain capabilities are added in the consuming app. For example you won’t get any nasty surprises if you add the Share library and not the Proximity capability – the library won’t include the “Tap+Share” option.

Beyond the other Charming libraries you can use this library yourself – for example write a generic About page which is populated at runtime by the app metadata. The library is tiny, you won’t need to mess about with XML parsing and it gives you the potential of sharing more code between Phone and Store projects.

ApplicationModel is the only one of the Charming libraries which doesn’t expose any UI. As already stated it is used by Charming Share, and also Charming Settings and as of 8.1 it will also be used by Charming Search but more of that in the next post…

A Trip Around Charming Share

The Charming libraries for Windows Phone are a set of (currently) six components for Windows Phone development with the mission of providing more code consistency with Windows 8. Following on from the NFC component, this post will cover its cousin the Share component.

Windows Phone provides a number of classes in the Windows.ApplicationModel.DataTransfer namespace but there is no Share charm or sharing functionality here. The Charming Share library replicates this functionality with a sharing component which can be called in the same way as on Windows 8 and yet exposes the various platform sharing mechanisms. Because this is not an OS feature it is not possible to create share targets and share data with other apps. I am looking at the possibility of making the providers extensible so that you can add items to the list of targets.

You can currently share three types of object – Text, Uri and Image. The available targets will vary depending on the data you pass. Image is a special case as it uses the ShareMediaTask which means you have access to all the system targets including other apps registered as Photo Extras. This also means you can share an image via Email which is not possible with the EmailComposeTask. The table below shows the supported targets based on data type:-

 

Text

Uri

Image

Tap+Send

 

Yes

Yes

Bluetooth

 

 

Yes

Messaging

Yes

Yes

Yes

Email

Yes

Yes

Yes (Individually Listed)

Social Networks

Yes

Yes

Yes (Individually Listed)

Photo Extras

 

 

Yes

 The Tap+Send functionality for Uris is provided by the separate Charming NFC Share component. It will only appear in the list if you add the NFC library as a reference in your project (and add the Proximity capability). The share dialog shown when passing a Uri is designed to look familiar and uses page transitions from the Phone Toolkit:-

I’m working on localised resources for these components so that they will be supported in all Windows Phone 8 languages. Because the Image sharing dialog is system provided this is already localised into the device language. In early testing the NFC and Share components are looking good with localised text and supporting LeftToRight and RightToLeft layouts.

If other formats can be supported I’m looking at the possibility of adding a Bluetooth provider to beam files using OBEX, again trying to stick as closely to the system provided Bluetooth sharing for images. Another possible target is a Clipboard provider for Text or Uri, the source code is in there but the provider is not currently used.

Page Header Styles

The page header is the small text you see across the top of most apps, often in all uppercase. When you create a new Windows Phone app from the default templates you’ll get something like this created in your MainPage.xaml

<TextBlock Text=”MY APPLICATION” Style=”{StaticResource PhoneTextNormalStyle}” Margin=”12,0″/>

The annoying thing about this is this is not the same format as built-in apps. The font size is wrong and so is the weight. Rather than manually fixing up every instance I define the following style in the App.xaml:

<Style x:Name=”PhoneTextPageHeaderStyle” BasedOn=”{StaticResource PhoneTextNormalStyle}” TargetType=”TextBlock”>
   <Setter Property=”FontSize” Value=”{StaticResource PhoneFontSizeMedium}”/>
   <Setter Property=”FontFamily” Value=”{StaticResource PhoneFontFamilySemiBold}”/>
</Style>

Then my page XAML becomes:-

<TextBlock Text=”MY APPLICATION” Style=”{StaticResource PhoneTextPageHeaderStyle}”/>

My app is now consistent with built-in apps!

[Updated with simplified Style XAML removing hard-coded font size]

Sending an Email with an Attachment on Windows Phone (Sort of)

From it’s initial release Windows Phone has had a method to programmatically send an email from code. In keeping with the mantra that the user is in control it has always been a method to populate the email but allow the user to decide to modify, send or cancel. Despite much demand the ability to attach files has not been made available. As a user you can attach a picture to an outgoing email but the phone can receive a wider variety of types. There is, albeit not as obvious, a method to send a picture attachment via email but there are caveats.

The ShareMediaTask only allows you to specify the image to use and not the transport to send via or an accompanying message. In fact it basically is just running the “share…” menu found in the built in Pictures app. The other caveat is that the picture must be in a location that the system can read from so a path to your isolated storage won’t work. You can however copy an image to the user’s pictures library. Another annoyance is that once you’ve copied a picture to the library you can’t delete it – only the user has the ability to do this.

Microsoft.Xna.Framework.Media.MediaLibrary ml = new Microsoft.Xna.Framework.Media.MediaLibrary();
Microsoft.Xna.Framework.Media.Picture pic = ml.SavePicture(“mypic.png”, stream);
path = Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions.GetPath(pic);

The above code takes an image stream and placed it in the library and gets a true file path. The code to initiate the share operation is equally simple:-

Microsoft.Phone.Tasks.ShareMediaTask shareMediaTask = new Microsoft.Phone.Tasks.ShareMediaTask();
shareMediaTask.FilePath = path;
shareMediaTask.Show();

That’s all there is to it – you don’t get any feedback on what option was chosen (or whether the operation was cancelled).

I’ll be discussing some more around sharing various types of data at my talk on this coming Tuesday for WPUG Manchester which is titled “Bluetooth and NFC for Windows Phone and Beyond” where I’ll be unveiling some ways to make “Charming” apps for Windows Phone.

Deployment Parts in a Windows Phone App

My apologies in advance as this is a little bit of an obscure scenario but I thought I’d share this information in case others found it useful.

Imagine if you will the following scenario. You have a dll project which can make use of other “provider” libraries. These are optional and the main dll should function regardless of whether the provider is present or not. The issue is this – you need to detect from your library whether this provider dll is present at runtime. In traditional .NET you would use reflection but not all aspects of this are available in the Silverlight model. Instead there is a way to interrogate all the assemblies within the XAP from the Deployment class. The following code snippet shows how to check for a particular assembly:-

bool featurePresent = false;

foreach (System.Windows.AssemblyPart part in System.Windows.Deployment.Current.Parts)
{
   if (part.Source.ToString().Contains(“FeatureAssemblyName”))
   {
      featurePresent = true;
      break;
   }
}

Launching PDF Documents

In Windows Phone 8 the Windows.System.Launcher class allows you to open files with apps which are registered to handle them. On Windows Phone you have built in support for Office documents and audio and video formats. Lets say you want to open a PDF either remotely or downloaded by your app. There isn’t a built-in PDF viewer but there are several available including free readers by both Microsoft and Adobe. If your user doesn’t already have one installed there are some extra steps. The first thing that happens is that the user is prompted like so:-

If the user taps yes the phone will display a store search for apps which are associated with the file type:-

 

Not sure why Adobe Reader isn’t in this list and why it looks like the last entry is a game 🙂 but at least the top couple of entries seem like they will do the job.