Category Archives: Tips&Tricks

Application Insights in Windows Phone 8.1 Silverlight app, first steps

Update! Microsoft has decided to kill the support for Application Insights for Mobile Apps and recommends switching to HockeyApp platform, which has much worse pricing options and not ready support for app analytics and not ready support for UWP apps. I am REALLY disappointed with this decision, because I’ve already integrated App Insights in about 7 of my apps and I was really satisfied with the results I am getting.

 

Application Insights (AI) is a new Azure-based analytics service available for variety of platforms like ASP.NET, J2EE, Android, iOS and Windows/Phone, and because I started having recently problems with my current analytics service Flurry I decided to replace in one of my Windows Phone 8.1 Silverlight apps Flurry with AI, for the start.

In this article I’ll cover the basic setup of Application Insights in Windows Phone app and also cover few specific problems that I ran into and I had to solve myself.

First steps

tl;dr for adding Application Insights to WP app follow Application Insights for Windows Phone and Store apps 🙂

Or these steps, first on Azure:

  1. For using AI you need to have Microsoft Azure account, if you don’t have one, you can start Free one-month trial.
  2. On Azure Portal create new AI instance using Create -> Developer Services -> Application Insights.
  3. Here choose a name for the AI instance, application type (ASP.NET, Windows Phone, iOS, etc.), resource group and subscription. Note AI is available in Free Tier as well for small apps so you don’t need to pay a single cent for using itVisual Studio Application Insights Pricing.
  4. Once you create your AI instance, click on Settings and here on Properties. Here copy your INSTRUMENTATION KEY which serves as unique ID of your AI instance in your app.

Now lets open Visual Studio to connect your new AI instance with your Windows Phone app.

  1. Open your Windows Phone 8.1 Silverlight app solution either in VS2013 or VS2015. Note VS2015 has built-in support for managing AI. In VS2013 you can install AddIn to get similar features, but from what I experienced this AddIn in VS2013 did not worked at all, it was throwing strange exceptions when creating new projects with AI or adding AI to existing project, so I highly recommend using VS2015 RC.
  2. In your project open NuGet and search for Microsoft.ApplicationInsights.WindowsApps and install it. Current stable version of AI for WP in NuGet is 0.17.
  3. Now open the new config file ApplicationInsights.config and here add here add your instrumentation key into this tag:
    <InstrumentationKey>{your.key}</InstrumentationKey>
  4. The last step is adding reference to the TelemetryClient into your app so the AI is actually started somewhere, and here comes the first problemI had no idea how and where to initialize the telemetry client. It’s not mentioned anywhere in the Application Insights for Windows Phone and Store apps, maybe it’s in subsequent articles, but I haven’t searched any further.

AddOn vs Manual Steps

When adding the Microsoft.ApplicationInsights.WindowsApps NuGet package to your project it tries to modify your App.xaml.cs file with the initialization calls. The thing is it somehow does not work even when I tested it with new clean project. Not sure, where’s the problem, really.

Since the manual guide failed in the last step, I tried the other way for initializing my project with AI – the automated way. In VS2015 it’s possible to just right click your project and click on Add Application Insights Telemetry… which adds all required NuGet packages and initialization calls automatically. This added all packages in WP8.1 Silverlight project but not the initialization calls.

I’ve tried it again this time on WP8.1 XAML project and it worked adding this code the the App.xaml.cs file:

/// <summary>
/// Allows tracking page views, exceptions and other telemetry through the Microsoft Application Insights service.
/// </summary>
public static Microsoft.ApplicationInsights.TelemetryClient TelemetryClient;
...
public App()
{ 
TelemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();

And when launched it works, as expected!

WP8.1 Silverlight App Initialization

So I figured out I’ll do the same in WP8.1 Silverlight project, and here comes the other tricky part. When I added the same calls to App.xaml.cs to the top of the constructor, the app crashes on NullReferenceException when calling the TelemetryClient constructor.

bug1

I tried to search some insight regarding this issue on Google and StackOverflow, but with no success. I was actually surprised there were no tutorials yet from WPdevs solving this problem, or maybe I am having problem no one else had?

So the solution was simple, I started JetBrains dotPeek disassembler and checked the Stack Trace where this crash happened and discovered in about 5 minutes the root cause:

bug2

PhoneApplicationService is here used before it’s initialized in App.xaml.cs, rookie mistake 🙂
PhoneApplicationService is defined as a service object in App.xaml and it’s available only after the InitializeComponent(); call. When I placed the TelemetryClient constructor before it, then PhoneApplicationService.Current is null. When placing it after InitializeComponent();, it works.

The summary

First create your Application Insights instance on Azure, then add the Microsoft.ApplicationInsights.WindowsApps NuGet package to your app, configure instrumentation key in ApplicationInsights.config and last create instance of TelemetryClient in your app.

If you are Windows Phone developer – in WP Silverlight app you need to place the TelemetryClient constructor after the InitializeComponent(); call, otherwise it will crash right after start.

If you are Windows developer targeting new Universal Apps platform, you can place the TelemetryClient constructor right in the top of App constructor with no problems. That’s because PhoneApplicationService is not used here.

And if you are by any chance Application Insights developer:

  • Make sure all tutorials contain required steps regarding TelemetryClient constructor placement.
  • Place the critical sections throwing NullReferenceException into try-catch, or just check if it’s null, and tell the user in Exception message where should the TelemetryClient constructor be placed
  • Revise all NuGet packages so that they place TelemetryClient constructor on proper places and if it’s not possible to place it, notify user.

At the end Application Insights now work in my Windows Phone 8.1 Silverlight app, maybe I’ll publish another blogpost later after I gather some useful analytics data 🙂

Using ResW File Code Generator in Visual Studio 2015

When developing apps for Windows 8.1, Windows Phone 8.1 or Universal Windows apps, the recommended approach for app localization is to use .resw resource files located in Strings/[culture code]/Resources.resw path. This approach is quite similar to the previous .resx localization system used in .NET/Silverlight apps with one big difference – there is no generated code-behind file now that you can use for compiletime-safe access of localized strings. If you want to get the string named “ListHeader”, you need to access it like this:

var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
var str = loader.GetString("ListHeader");

As you can see this is not exactly convenient.

Simple solution for this problem is to use the ResW File Code Generator extension for Visual Studio. All you need is download the ReswCodeGen.VSPackage.vsix file and install it.

Now when you open properties of any .resw file in your app and enter ReswFileCodeGenerator in the Custom Tool field, the code behind file will be created automatically. It’s also possible to define the namespace of the generated class using the Custom Tool Namespace field.

Capture

 

Now it’s possible to access the string ListHeader like this:

var str = Vlak.Resources.AppResources.ListHeader;

Not just this is much shorter to use, it’s also safer. If you now rename the property to ListTitle or delete the string from resources, the app won’t compile, which is good, because you know immediately that the string is not in resources anymore. With the original approach using loader.GetString the app would launch but throw an exception when accessing the no longer present string.

 

ResW File Code Generator in Visual Studio 2015

Installing the addon to Visual Studio 2012 or Visual Studio 2013 is supported out of the box. The problem, is when you want to install this addon to Visual Studio 2015 – it won’t work using the available installer. Update, new version of ResW File Code Generator for Visual Studio 2015 was released just day after my article, strange coincidence 🙂

Luckily there is a simple fix for this:

  1. Download the ReswCodeGen.VSPackage.vsix addon, rename it to ReswCodeGen.VSPackage.zip and extract it as a zip file.
  2. Open the extension.vsixmanifest file in any text editor.11 
  3. Change the line
    InstallationTarget Version=”[11.0,12.0]”
    to
    InstallationTarget Version=”[14.0]”
    and save the file.
  4. Pack again the content of the folder as a zip archive and name it ReswCodeGen.VSPackage.vsix22 
  5. Now if you double click the new ReswCodeGen.VSPackage.vsix installer, it will install as expected into Visual Studio 2015 just like the original installer in Visual Studio 2013.

That’s all, simple solution for better productivity when developing localized Windows Store apps that you can start using right now even in Visual Studio 2105!

New API in Windows Phone 8.1 Update 2

Windows Phone 8.1 Update 2 v 8.10.15127 finally arrived as the default OS version on new Microsoft Lumia 640 and 640 XL, and again, there is a new hidden API in it, so let’s take a look, if there is anything interesting that we, developers, can use in our apps.

tl;dr The API diff between WP8.1 Update 1 and WP8.1 Update 2 is here on GitHub. Only added/updated Windows.Phone.Notification.Management API – nothing we can use in our apps without special Accessory extension SDK, + new property in PhoneApplicationPage,

Continue reading

How to fix missing Extension SDKs in Windows 10 Universal Apps

When tools for developing Windows 10 Universal Apps were released couple of days ago I started porting one of my apps to find out, what are the changes and possible issues.
First issue I ran into was that I was unable to add Behaviors SDK to my project – the list of Extension SDKs was almost empty:

In Windows/Phone 8.1 project:

sdk.8.1

In Windows 10 UAP project:

sdk.10

Luckily, there is a workaround, according to Release Notes for the first version of Windows 10 Tools it’s necessary to copy the extension SDKs from the Windows 8.1 Extension SDK location to the universal apps location—that is:

From:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1\ExtensionSDKs
To:
C:\Program Files (x86)\Microsoft SDKs\UAP\v0.8.0.0\ExtensionSDKs

After restarting Visual Studio 2015 the copied SDKs are then available in the list and you can add it just like before. Done.

#WPdev Quick tip – API for accessing Windows Store from your app

Following the previous WPdev quick tip how to initiate Email Compose from your app, here is another API that developer often need:

  • Navigate to Rate and Review dialog for the current app – if you want to initiate app rating from the current user
  • Navigate to Windows/Phone Store to the currently running app – if you want the user to purchase full version of current app or upgrade to a new version
  • Navigate to Windows/Phone Store to all apps published by the current developer – if you want to encourage the user to download other of your apps

Unfortunately all these tasks require different type of API in Windows Phone 7.5/8/8.1 Silverlight apps, in Windows 8.1 apps and another one in Windows Phone 8.1 WinRT apps and since I needed all these methods in my recent Windows Universal App NASA TV Live, I decided to put it all into short article for future use, so here it is:

Continue reading

#WPdev Quick tip – API for sending emails from your app

When developing Windows Phone or Windows Store app it’s often useful to give your user option to send you feedback email or feature request. In this short #WPdev article I’ll cover all three different approaches you could use in all current Windows Store platforms.

Windows Phone 7, 7.5, 8 and 8.1 (Silverlight)

Sending emails from original Windows Phone 7+ Silverlight apps is done using the EmailComposeTask:

string to = "my@email.cz";
string subject = "Idea for your app";
string body = "Hi, here's an idea for your cool app...";
EmailComposeTask emailTask = new EmailComposeTask
{
 To = to, Subject = subject, Body = body,
 //Cc = , Bcc = , CodePage = ,
};
emailTask.Show();

When the Show method is called the system dialog appears where you can choose the mail account that will be used for sending the email.

As you can see the usage is really simple, one class and one method is all you need. Note there is no way how to send emails with attachments using this API.
Another problem also is that this API is no longer available when developing Windows 8/8.1 or Windows Phone 8.1 WinRT apps. So what is the alternative?

Continue reading

How to deploy your own NuGet server on Azure Website in 15 minutes

Let’s imagine this typical scenario: You are an indie developer or a company developing apps in Visual Studio. You have already developed and use several own useful libraries across various projects. Right now you are maintaining each of these libraries in separate Git/TFS repository and adding these libraries as submodules to your projects. Because these libraries are hierarchically dependent one on another, it’s quite hard to maintain the dependencies and changes properly. You’ve thought about packaging these libraries as separate NuGet packages, but adding them to public nuget.org gallery is not an option since these libraries contain private knowhow.

In this article I’ll demonstrate, how to deploy your own NuGet Server on free instance of Azure Website, secure it with Basic HTTP Authentication, setup this new server so it can be used from Visual Studio, easily create automatic PowerShell scripts for deploying these libraries as NuGet packages to your server, use these NuGet packages in your projects and also how to achieve all this in just couple of minutes!

Continue reading

New API in Windows Phone 8.1 Update 1

Accessory-App-Windows-Phone-348x620As a part of the “Preview for Developers” program Microsoft just recently released the first larger update for Windows Phone 8.1 called just “Windows Phone 8.1 Update” with OS version number 8.10.14147.180. The original Windows Phone 8.1 was 8.10.12359.845.

According to MSDN blog article this update brings primarily these new end user features:

  • Cortana is now available for couple of new countries
  • Live Folders – native implementation of app folders on the Start screen
  • Option to simply select multiple SMS messages for deleting/forwarding
  • Apps Corner, Private VPN, Store Live Tile, Internet sharing via Bluetooth, customizable snooze time for Alarms and more.

But probably the most interesting feature, that might be part of this update as well, is is the support for notification on accessories, typically on smart watches and other wearables. The best hint for this functionality is already in the phone settings – the new “accessory apps” section.

The second hint is the new available API, that’ll be shown in the rest of this article.

Continue reading

File IO Best Practices in Windows and Phone apps – Part 1. Available APIs and File.Exists checking

Working with files is one of the most common tasks when developing any Windows Phone or Windows apps. In this mini-series of articles I’ll show you available APIs in all Windows Phone and Windows versions, caveats when doing task such as checking if specific file exists, getting target file, reading writing data from/to files and also how to effectively extract data from ZIP files, and more.

In the first part of this series I’ll discuss available File IO APIs in Windows Phone 8, 8.1 Silverlight, 8.1 XAML, Windows 8 and 8.1 platforms and benchmark all available methods for testing, if target file in Local Folder exists, or not. To my great surprise, the performance of methods in StorageFolder API is dramatically different across different platforms and it’s not a good idea to use single approach everywhere.

Continue reading

Xbox 360 Controller API for Windows Store apps

In Windows Store apps for Windows 8.1 there are four basic input methods – touch, mouse, stylus and keyboard. In most of scenarios these input methods are all you need, but in case you want to offer more natural experience for controlling games on Windows 8.1, you can also add support for Xbox 360 and Xbox One controllers.

In this dev article I’ll show you, how to use in any C#/XAML app or game for Windows 8.1 the Xbox 360 Controller API, how to detect presence of such controller and how to poll for current state of all buttons.

Continue reading