Monday, 2 June 2014

Universal Apps: partial understanding

One of the ways to build a universal app is to use the partial keyword to allow you to split out functionality that must be different between platforms.

We tried this with our error reporting code, loosely based on LittleWatson.

There is good documentation here http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.90).aspx on partial classes, but despite what it said we just couldn’t get it to work correctly.  So we concocted more and more bizarre ways to use partial, so that the program would compile, including partial methods (which unless implemented the compiler optimizes out).

In the end, it turned out to be nothing related to partial at all, that was preventing our methods from being called.  What had actually gone wrong was this:  We had inadvertently put half of the partial class in a different namespace, thereby making it invisible.  It’s no wonder we couldn’t find member fields in our main implementation.

Pitfalls of adding WP8.1 support to a universal app (Part 1 of N)

As part of our continuing port from Windows 8.1 to WP8.1 of Surface Level, I’ve just recently tracked down a problem thrown during suspension.

We’d already written quite a few classes that would act as a base class, and as a result, I implemented a partial method which I could call for certain tasks.  One of these tasks was registering our Suspension handler.

The bug I was seeing was an end of file reached while reading back our state file, which we use when we resume.  But there were two problems

1.  I kept getting odd suspension manager exceptions in my crash logs.
2.  The suspended handler never seemed to be called anyway.

Item 2 was the first to solve.  It appears that like W8.1, apps aren’t suspended when run in the debugger.  The only problem was, I couldn’t find the UI to shut it off.

Well, eventually I found it here on the Debug Location toolbar:

image

Clicking Suspend actually caused my suspension to run (twice in fact)

The first crash was caused by the state file being read only (a side effect of running suspension twice).  It turns out that when I added the WP8.1 project, the wizard had added a second call wiring up the suspension code, which I hadn’t spotted.  Removing the duplicate wiring allowed me to get suspension working properly.

String.Format and Cargo Cult Formatting

While working on a port of our Surface Level app to a universal app, I stumbled across some exceptions being thrown in a great matrix library I picked up from here:

http://blog.ivank.net/lightweight-matrix-class-in-c-strassen-algorithm-lu-decomposition.html

Internally, the code was eventually calling

String.Format("{0,5:0.00}", v);


and the bug was caused by a leading space while parsing.


I’ve noticed this string before in some of the Microsoft accelerometer samples, but never actually looked at what it did.


It turns out the ,5 makes the field right justified, filling 5 characters, and padding with spaces.  In all of my testing up to now, I never saw this, but somehow on the latest device, I saw a value of 0, which ended up with a leading space.


The bug has now been fixed by the author in another way, but it points out the importance of actually reading every line of code, and understanding it before you put something into a project.

Sunday, 1 June 2014

Why is documentation so terrible these days?

I’m recovering from ankle surgery after a low speed skiing accident that I had in February, and have decided to take the opportunity to port our Surface Level to Windows Phone 8.1 that I wrote while recovering from the initial accident.

I have about 8 weeks where I’m unable to drive, so will be working from home and very bored, so as a result, will have plenty of time for the project.

One of the things I’ve noticed both for my apps, and my job is that the documentation supplied by companies these days appears to be getting worse.  For instance, in my day job, I’m currently building an embedded system based on an STM32F4 microcontroller, and hooking it to a variety of WIFI modules on the market (the only reason for the variety is we keep coming to problems that aren’t documented, and then prevent us from using a particular module in our product).

I’m not sure if the documentation is intentionally bad so that competitors can’t find the weaknesses, or if the product is bad because it’s rushed to market, or if it’s just pure incompetence.

So, back to Surface Level.  While building the Windows Phone 8.1 version, I added back some code from the working Windows 8.1 version, including an AppBar

 

image

A quick check of the docs shows it works, but then when I run the program I get this:

A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in SurfaceLevel.WindowsPhone.exe

WinRT information: Cannot create instance of type '%0' [Line: 17 Position: 17]

Additional information: The text associated with this error code could not be found.

Well, that’s a helpful message.  Line 17 does in fact have this on it:

<AppBar x:Name="BottomBar" Opened="OnOpened">

But position 17 is at x:Name?!

If you do manage to get all the way down the page in the online version of the documentation, you might find this:

image

Oh, I see.  I can’t use it.

Then I tried to mirror the front camera, like I do on the Windows 8.1 version, like this:

mediaCaptureMgr.SetPreviewMirroring(selectedDeviceInformation != null ? (selectedDeviceInformation.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front) : false);

That throws an exception, because the phone camera doesn’t support it (at least not on my phone), and there’s some helpful documentation in the remarks section:

Horizontal mirroring is useful for video conferencing or video chat applications, because the users typically want to see a reflected image of themselves. The non-mirrored view can look strange, because we are used to seeing ourselves in a mirror.

To mirror the preview video, apps should instead use the following method. Windows Store apps using JavaScript should use the msHorizontalMirror property of the video object. Windows Store apps using C++, C#, or Visual Basic should use the FlowDirection property on the CaptureElement.

SetPreviewMirroring can potentially be used as a performance optimization on some devices

So, we have a method that we shouldn’t use.

While the information is there, it takes at least 20 minutes to discover why things aren’t working by poring over the documentation for each and every method/function call, and my first language is American English.  I feel really sorry for people who aren’t native speakers.

Saturday, 31 May 2014

I don’t think the EU knows how the internet works

From January 2015, small software developers have a new administrative hassle to worry about: EU VAT

https://twitter.com/HMRCgovuk/status/472696531213381633

HMRC has issued guidance on the new VAT place of supply rules for digital services, coming into effect on 1 January (2015): http://ow.ly/xnvTt

If you’re VAT registered, and selling software B2C in the EU, you now have to account for VAT to the country where each individual belongs.  Have a read of section 1.5 of the above link, and see just how much fun this is going to be.

If you only get paid through an app store, you’re probably OK, as the app store will take the hit, but if like us you collect payments directly via PayPal, it’s going to become very difficult, unless we’re allowed to assume that the PayPal registered address is in fact the one that applies to the customer.  In that case, we can set a different rate of VAT for each country.

However, we still have to pay the VAT to each and every country it’s due to, instead of to a central EU body.  As a result, this will increase costs enormously selling outside of our home countries.  My prediction is that this will lead to a reduction in the amount of independent software available, as the overhead of filing in each country is not generally worth the returns from the sales to those countries.

VS2013.2 : One step forward, two steps back

We try to make it really easy to put together a new application by using the Export Template feature of VS2013 Pro.

Currently we have one template for all of our windows 8 apps, and another for our windows phone apps.

To start a new application, we use VS2013, and say File | New Project

Select our template and we’re off.  In that state we already have our view models, initialization and serialization frameworks, etc.  All of these are built from source files, and aren’t in a class library anywhere, and using templates makes it very quick to create a new independent branch.

We thought that with the advent of the new Universal Apps, we might be able to solve these problems once and for all, however, it isn’t working out like that.

The export template functionality is still there, but it doesn’t work.  You can export the shared project (but that doesn’t show up on the new project templates), and you can try to export either the WP or Windows Store projects, but they fail throwing an error about bad characters in the path.

So, from what I can see, we’ve lost this great functionality.

It appears that you could code up a template by hand by hacking through what’s in the universal app templates, but I’m not sure I can be bothered to at this stage.

With any luck, I’ll find a solution, and post it back here.

Saturday, 14 December 2013

WP7 Run Under Lock Screen Mystery

My son just returned from the Varsity Ski Trip, and used Skied Demon on a Windows Phone 7.  Now I know there aren’t many people out there who actually have one of these phones, and we rewrote the software to work while making phone calls on WP8, however, it still irks me when code we wrote doesn’t work for some of our customers.

The problem goes like this:

My son starts logging in the app, and then presses the power button to lock the screen and save power.

Later, he sees something that requires a photo and presses the camera button to wake up the phone.

After he backs out of the camera, he finds that the program is no longer running.

Of course, while on holiday, he had better things to do than tell his father an app isn’t working like he thinks it should be, but now he’s back, I’ve looked into the cause (30 minutes from bug report to investigation.  That’s how we like to treat all bug reports, so if one of our apps crashes, let us know, and we’ll investigate).

It turns out that the software is  getting a RootFrame_Obscured message, when he presses the lock screen.

When the camera button is pressed, an Application_Deactivated event is raised, my state is stored, and that’s the last my code ever sees of any execution time.

The program does have a background XNA thread for audio that’s raising a System.Threading.ThreadAbortException in Microsoft.Xna.Framework.dll before it exits however.

Any suggestions?