Tuesday, September 25, 2012

WinRT ArgumentOutOfRangeException "No mapping for the Unicode character exists in the target multi-byte code page" from FileIO.ReadTextAsync()


The following code to read the text contents of a file in WinRT thru an exception on some txt files and not on others.

var localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await localFolder.GetFileAsync("Sample.txt");
var stringContent = await Windows.Storage.FileIO.ReadTextAsync(file);
An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

WinRT information: No mapping for the Unicode character exists in the target multi-byte code page.

Additional information: No mapping for the Unicode character exists in the target multi-byte code page. (Exception from HRESULT: 0x80070459)

If there is a handler for this exception, the program may be safely continued.

Solution 1 - Remove all the problem characters

The following line in the txt file was causing the issue:

Rangitoto ki te Tonga/D’Urville Island

Changing the to a ' resolved the issue.

In another text file I also needed to change a to -.

Solution 2

Change the file encoding using Visual Studio. When I opened the file it had the encoding: "Western European (Windows) - Codepage 1252"

  1. Open the file in Visual Studio
  2. File > Advanced Save Options... >
  3. Change the encoding to "Unicode (UTF-8 with signature) - Codepage 65001"
  4. Save the file

Wednesday, September 12, 2012

Windows Phone 7 - Compass Bearing

About Compass Bearing for WP7

Shows true north and magnetic compass bearing as reported by the phone. Using your location data the app will also show a calculated true north bearing with independent magnetic declination calculation based on your latitude and longitude.

In vision mode you get an augmented reality compass that can be switched between the phone and app true north readings.

Tapping in the center of the compass dial will toggle between:

  • app true - The bearing for true north calculated using the reported magnetic north bearing and the magnetic declination for your location.
  • magnetic - Magnetic north as reported by the operating system. Theoretically this should point in the same direction as a physical compass would. There are known bugs with Windows Phone 7 handsets that can cause this to point in the wrong direction.
  • phone true - True north as reported by the operating system.

Trial mode is fully functional.

Tips and FAQ

The compass isn't pointing North

There are a number of factors that could cause an incorrect compass bearing.

  1. Calibration
    Within your handset is a magnetometer sensor. This sensor measures the strength of the magnetic field and reports the values via the phones operating system. It needs to be calibrated from time to time to ensure correct operation.
    One way to do this is to move it close to another magnet so that the calibration screen appears. Then move the phone away from the magnet and rotate it around all the axis.

    Hold the device in front of you and sweep it through a figure 8 pattern as shown until the calibration is complete.

    Another option to force calibration is to turn the phone off and then on again.
  2. Interference
    A number of objects may interfere with the magnetometer readings. Try moving away from magnets, other electrical devices (especially speakers), large pieces of metal, etc...
  3. Access to location information
    The Compass bearing app requires access to your location information to correctly determine the magnetic declination for your latitude and longitude.

Still not working?

If the issue still persists, can you please provide me some further details:

  • What is your approximate latitude and longitude or country and city of origin. These don't need to be exact, just sufficient so I can check the magnetic declination for your location.
  • Could you send me a screen capture from Raw Compass Data running on a level surface? There is a dedicated capture button in the bottom right that will save a screenshot to the picture library.

Known Issues

v1.4.0 There is a bug that can cause an exception when starting vision mode. There is currently a fix for this going through certification and it should be available soon.

See also:

Sunday, September 9, 2012

WP7 Application causes the onscreen keyboard to stay on

Setting focus to a TextBox immediately after calling MessageBox.Show() can cause the onscreen keyboard to get stuck on if the user presses the devices start button while the prompt is still displayed.

Once in this state you need to find an application that will display the keyboard so that it can be dismissed normally.

For example, rather than doing something like:

if (string.IsNullOrEmpty(ivm.Name))
    MessageBox.Show("Please enter a name");
    // This line will still occur if the user presses the hardware start button while the message box is displayed and will cause the keyboard to appear on the start screen.

Instead, set the focus before showing the message box so the keyboard can be dismissed by the OS code when the hardware button is pressed.

if (string.IsNullOrEmpty(ivm.Name))
    MessageBox.Show("Please enter a name");

Friday, September 7, 2012

TechEd 2012 Round Up / Summary

I've summarised some of the most interesting/important parts of my TechEd 2011 NZ notes here.

Keynote - Part 1 Part 2

DEV201 What's New in Microsoft Visual Studio 2012

Presenter: Adam Cogan @AdamCogan

DBI201: What's New in Microsoft SQL Server 2012

  • Concat - implicit cast. Null handling.
  • Try_Convert - return null rather than exception is cast fails
  • Parse - Returns the result of the expression, translated to the requested data type.
  • Format - like .NET format [23:00]
  • EOMonth - End of month [25:00]
  • DateFromParts - Build a data from parts
  • Throw [33:40] - rethrow the exception
  • sp_describe_first_result_set - types from the result set [38:00]

APP207: Windows UI Design

DEV301: Asychronous Programming in Visual Studio 2012

Presenter: Ivan Towlson

  • Challenges
    • Code readability - async can break up the logical flow
    • Exception handling - Where did the exception occur.
    • Resource management - placement of using statements, etc...
  • async
    "This method may return before is has finished".
  • await contextual keyword.
    "Return from this method for now*, but resume it again when the following operation has completed"
    * Maybe
  • The await keyword will ultimately strip off the asynchronous Task without blocking the calling thread.
  • Exception handling - Centralized exception handling. Very similar to how the synchronous method would be written. Don't need a separate exception handler.
  • CancellationToken, CancellationTokenSource - OperationCanceledException
  • await Task.WhenAll(downloads); //A task that becomes complete when all of the sub tasks are complete.
  • Inside await
    - Split method into synchronous blocks.
    - Compiler creates glue code for GetAwaiter()
    - Awaiter has a OnCompleted callback, GetResult(), and IsCompleted.
  • await Internally uses StateMachine
  • What async/await Isn't
    • A Way to do parallel processing - Use Parallel LINQ or the TPL
    • A way for multiple concurrent operations to coordinate - Use agents
  • Shifting computations in time
    • Futures (Task) computations in progress who whose results will become available at some point.
    • Delays (Lazy) computations which will be delayed until the results are needed (but performed sync)
      "defer the creation of a large or resource-intensive object or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program"
    • Promises:
    • Agents: concurrent objects which communicate asynchronously and are internally serial
  • Summary
    • Blocking calls in naughty.
    • async and await allow you to write async/good programs that are as easy to write and to read as sync/bad ones.
  • See also:

    INO101:Microsoft Future Vision

    APP202:The Absolute Beginner's Guide To Building Windows 8 Apps In C# and XAML

    Presenters: Ian Randall @kiwipom and Ben Gracewood @nzben

    • Metro Design Principles
    • Pixel (device independent pixels)
      Sub unit - 5x5 pixels
      1 Unit - 20x20 pixels
    • XAML Spy - HTML DOM browser equivalent for XAML
    • Spacing - 1/2 unit (10 pixels) between similar elements.
    • Font Ramp: Header, Sub Header, Small Sub Header, Body Text
    • Fast and Fluid
      - Motion
      - Responsiveness
      - Get off the UI thread
      - Wait 25 ms when arriving at the page before loading content. Displays initial page faster.
    • Authentically Digital - Avoid skeuomorphism

      "The details and design should focus on function rather than trying to emulate real-world analog versions of things, like the animation of a page turning in an e-book."
    • Use Contracts to integrate with the rest of windows.
      You don't need your own independent settings or Search functions.

    APP201:From Lazy to Metro - A Journey on Windows Phone and Windows 8

    DEV304:Engaging the Realtime Web with SignalR

    SIA201:Hack-Ed: From the Trenches

    Presenters: Andy Prow and Kirk Jackson

    • Back Tracking on a RegEx and consume CPU time. Place time limits on execution.
    • Consider segmenting the authentication system from the core database to protect against SQL injection exposing data.

    APP310: Bring Your Windows 8 and Windows Phone Apps to Life with Push Notifications using Windows Azure

    Presenter Nick Harris @cloudnick

    • Windows Push Notification Service (WNS)
    • Types of tile template variation: Text only, images only or a combination.
    • Badges for tiles
    • Use Toast Templates to inform the user of something important.
      The best toast notifications are personally relevant and time sensitive.
      Don't over use toast notifications.
    • Each tile has a unique Channel URI. Requested by App on each run. URI can change. Typically expires every 30 days. Maybe purged sooner. Generated by WNS.
    • PushNotificationChannelManager win8
      HttpNotificationChannel.Find WindowsPhone7
    • ToastContentFactory from Nuget
    • If building your own the WNSRecipe and NPNSRecipe help

    APP204:From Web to Windows - Designing Modern Windows 8 Apps using HTML, CSS and JavaScript

    Presenter: Gus Pickering, Sam Langley and Zach Hitchcock

    • Application Experience Review (AER) was used for early apps. Highly recommended to go through if you can.
    • Expression Encoder for screen captures.
    • Ensure you include a privacy policy (required if you have internet capability) and a support email address in the app.
    • Typically use 7+ for the age limit unless there is a reason to set it lower.
    • Many different ways to imply a grid, color, size, proximity, shape,
    • User Experience:
      • Make your hit areas bigger
      • Snapped view is important
      • Include portrait (unless good reasons not to)
      • Use semantic zoom
    • Skeuomorphism - making things look like their real world counterpart.
    • For testing - Use a second portrait monitor to test orientation. (or the simulator)
    • Learning JavaScript as a programming language
      JavaScript: The Good Parts. Douglas Cockford.
    • JavaScript Libraries
      Using existing libraries will probably contain unnecessary compatibility code for other browsers.
    • Useful Tools:
      • Visual Studio 2012 - JavaScript Console, DOM Explorer, Simulator
      • Fiddler.
        - Composer - send Get/Put requests to a URL Manually put in values.
        - AutoResponder - fiddler intercepts and responds with your predefined response
      • Javascript libraries
        - Underscore.js - Give functions similar to Linq queries in JavaScript
      • JavaScript
        - Mozilla Developer Reference
        - Be disciplined with conventions and testing to avoid issues due to lack of typing system. Write defensive code for type checking.

    APP205:Practical Metro - Build Stunning and Innovative Windows 8 Apps Rapidly

    DEV305:Enjoy A Cup Of CoffeeScript

    DEV401:It's .NET, Jim, but not as we know it...

    Presenter: Ivan Towlson

    • Windows Runtime C# classes must be sealed. Can't have generic classes in a public interface. Internal components can.
    • Windows Runtime C# structs can only contain public fields, and nothing else. A C# class can't contain public fields.
    • You can't create your own public exception classes, only use the predefined sealed type.
    • You can't have ref parameters in a public method.
    • These restrictions don't apply to pure .NET libraries. Only those targeting the Windows Runtime.
    • Javascript doesn't support inheritance. Hence all public classes need to be sealed. They can do it internally as they can code special cases (projections?) in WinRT.
    • Portable class library (.NET class library) that can be consumed by and .NET runtime and .NET in WinRT.
    • .winmd files. MetaData files for the runtime. Can be opened by IL DASM.
    • Catalog of all the classes used in an app are stored in the registry.
    • Windows Runtime is built on COM.
    • Language projections generally protect you from COM. Hence can't define own exception types as that is all the COM supports.

    APP206:The Interaction Continuum

    Presenter: Mark Manderson

    • As you move further from the screen it is harder to do content creation. Becomes more about consumption.
    • Covered pros and cons of:
      • Voice Recognition:
        + Control area is large
        + Very little user training required
        + A natural communication mechanism
        - Machine may need training to be effective
        - Natural human speech patterns are hard to parse
        - Noise cancellation required
        - High machine load
        - user frustration
      • Depth Cameras:
        + Skeletal/Hand tracking for posing and gestures
        + Face and Head tracking
        + Recognition of a specific user
        + Interaction at medium distance
        + Natural Gestures
        - Can only show a limited amount of information.
        - Can require exaggerated movement
        - A heavy software layer
        - Occlusion - More than one user
        - User Fatigue
      • Sensor Fusion:
        + Can infer environmental factors
          - Orientation, location, movement, etc...
          - Environmental effects on user
        E.g. Augmented reality systems.
      • Touch:
         + Now cheap and ubiquitous
         + Windows 8 is very touch centric
         + Natural User Interface
        - Fat Fingers
        - The Ghost touch - quirks of the sensor technology
         - "Gorilla Arm" - Sore arms after extended usage.
         - Lack of haptic feedback - no physical feedback.
      • Direct Input Mechanisms:
        + Used where precision is required
         + Great for content creation
         + Allows economy of movement - reduces fatigue..
        - Must be very close to the machine
        - OOS/Repetitive strain injury
         - Considerable training can be required
         - Not natural - an abstraction

    APP301:Everything You Need to Know About Building REAL XAML Apps for Windows 8

    Presenters: Ben Gracewood and Nigel Sampson

    Demo source code.

    • Use device independent pixels to allow scaling. Gives independence from the device resolution.
    • XAML Spy, select visual elements
    • Support for Snap View "Snapped" and portrait
    • /Common/BindableBase.cs - CallerMemberName Attribute to implement INotifyPropertyChanged. Also CallerFilePath and CallerLineNumber.
          public sealed class Logger
              public static void TraceMessage(string message,
                                       [CallerMemberName] string memberName = "",
                                       [CallerFilePath] string sourceFilePath = "",
                                       [CallerLineNumber] int sourceLineNumber = 0)
                  Console.WriteLine("message: " + message);
                  Console.WriteLine("member name: " + memberName);
                  Console.WriteLine("source file path: " + sourceFilePath);
                  Console.WriteLine("source line number: " + sourceLineNumber);
    • DefaultViewModel (from File > new project) is just IObservableMap<...> this.DefaultViewModel["Groups"] = //...
      Typically the first thing to throw away from the default templates.
    • ReSharper gives intellisense for StaticResources.
    • SemanticZoomView - Give more information in the zoomed out view than just square tiles if possible.
       - Not an optical Zoom
       - If your grid is grouped then have semantic zoom.
       - Pretty much impossible to extend
       - A lot of scope for creativity
       - Can be more than just navigation
      Currently only one level of zoom. Should continue to show the same information at the same level. 
    • Async/Await [41:30]
      override OnUnhandledException
      In async void methods an unhandled exception gets put onto the default stack pool and causes the application to crash out rather than handle the exception.
      Anywhere there is an async void ensure you add exception handling as OnUnhandledException won't catch it.
    • Helper method. HandleErrors to wrap any async void. [46:30]
      private async void HandleErrors(Func<Task> action)
                      await action();
                  catch (Exception ex)
              private async void DisplayError(Exception ex)
                  var dialog = new MessageDialog(ex.Message, "Unhandled exception in Application!");
                  await dialog.ShowAsync();
    • If the method returns Task<T> ensure there is an await in the call or any exceptions will be swallowed up.
    • Code52 tool to reset highlight colour - HAMMER.Pants.exe Change cool in theme resources. Take generated file (Generic.xaml) and add to App Resources folder. Include it with the other Resource dictionaries (In App.xaml > ResourceDictionary.MergedDictionaries).

    INO202:Building Roborazzi, a Kinect-Enabled Party Photographer Robot

    DBI305:Practical Uses and Optimisation of New T-SQL Features in Microsoft SQL Server 2012

    DEV311:Turbocharge your Productivity with Visual Studio

    Presenter: Bevan Arps

    • Environment - Light or Dark color theme.
    • Consider using a proportional rather than fixed width font.
    • Ctrl+, Search for objects, methods
    • Solution Explorer now has a search box. Solution Navigator in Visual Studio 2010 productivity power tools is the earlier prototype.
    • Visual Studio Test Explorer has adapters for running nunit and other testing frameworks.
      Option to automatically run tests after a build.
    • Create a custom ruleset for code analsys and then save the .ruleset file at the root of the project. Create a separate build configuration that does a debug build and adds in the Code Analysis.
    • DebuggerDisplay attribute to change display of instance
      [DebuggerDisplay("Count = {count}")]
      class MyHashtable
          public int count = 4;

    DEV313:WebAPI: Know It, Learn It, Love It

    Presenter: Grant Archibald @garchibald

    • WebAPI is designed to work across HTTP. New Framework to easily Send/Receive data over HTTP to a range of clients.
    • Data format agnostic but usually JSON or XML
    • Easy to consume from JavaScript!
    • Add packages for help and odata.
    • Add a SecurityException Filter. Only return the top level exception message
    var client = new HttpClient(handler);
    client.DefaultRequestHeaders.Add("Accept", format);
    await client.GetAsync(url);
    await client.PostAsync(url, … JsonConvert …);
    await client.PutAsync(url, … JsonConvert …);
    await client.DeleteAsync(url);
     url: '/api/Members',
     contentType: 'application/json',
     dataType: 'json',
     type: 'GET',
     success: function(…) { },
     error: function(…) {}

    See Also:

    ARC401:Hack-Ed: Design for Attack

    DEV314:What's New for Unit Testing in Visual Studio 2012

    • Continuous Testing - Run Tests After Build
    • Support for different testing frameworks
    • Reduced number of windows to work with

    APP304:Using PhoneGap to Write Once, Look Metro and Run On All Mobiles

    Presenter: Kevin Isom - @kev_nz

    • www.phonegap.com
    • Allows you to make a mobile web application and deploy it to the suitable store for the device.
    • Also allows you to talk to the hardware. Camera, messaging, audio recording.
    • A weakness is animation support. Especially when using PhoneGap on WP7.
    • Still a Windows Phone App under the hood. Could easily create a hybrid application.
    • www subfolder can be common with other phone platforms.
    • Won't have support for touch events in WP7. Need to use click events, which are a bit slower.
    • If your app is your product, write native for each device.

    INO201:Building Robots - Getting Started with the .NET MF and .NET Gadgeteer

    APP305:Working with Windows 8 Sensors, Devices, Proximity and Near Field Communication (NFC)

    Presenter: Lewis Benge

    • Seamless access to devices under Windows 8
    • Play To for streaming to TVs. Industry standard DLNA.
    • CameraCaptureUI
      Control for built-in camera control with pre-defined user workflow.
      Similar to 'pick' photos or videos from the file system
      Specify quality of capture
      Set Timers
    • WIN8 minimum hardware spec
      Ambient Light
      Motion Sensor Fusion
      Windows Location Provider
      GPS (if mobile hardware is present)
    • Location
      Every single Windows 8 can provide location data (if the user allows it)
      Includes enrichment from bing maps.
    • Light-aware apps. Adjust for Dark, Indoors, Outdoors. Contrast adjustment on Visual State.
    • ST Microelectorincs eMotion Development Board
    • Near Field Communication (NFC)
      Under the Windows.Networking namespace.
      Tags are read/write. Not powered, no batteries.
    • Windows.Devices.Enumeration - Device discover and notifications about devices.
    • Windows RT devices don't expose low level bluetooh devices.
    • Companion app - automatically installed when hardware is added.
    • Windows 8 SDK samples - Media Capture

    DEV315:JavaScript - Not Your Mother's Scripting Language

    SIA302:Hack-Ed: Mobile Security

    APP208:Windows Phone: What's New

    APP303:Create Your Own Lego: Building Custom Controls for Windows 8 Apps

    DEV402:Multi-threaded Programming in .NET from the ground up

    Presenter: Orion Edwards

    • Interlocked methods - Atomic in nature. Increment, Decrement, Exchange, CompareExchange, Add [6:00]
    • The C# and JIT compilers only consider the single-threaded case when optimizing. Behaviour depends on the environment/platform. [13:27]
    • Volatile keyword - ensures the compiler doesn't optimise the the usage and create bugs. [14:19]
    • The CPU rearranges the order of reads and writes to memory! [16:00]
      Each CPU keeps track of it's own reordering, but doesn't inform the others. [19:30]
      • Use a lock - Standard Lock inserts appropriate barriers behind the scene.
      • Memory Barrier - Thread.MemoryBarrier
    • Memory Model x86 and x64 will only reorder "Stores reorders after Loads".
      ARM processors may do significantly more reorderings.
    • Lock-free programming
      Your data structure must be immutable - allows multiple threads to read concurrently
    • Lock-Free Performance benefits are hard to get. Almost always better off using locks
    • Threading Model
      What threads can access what data?
      What threads exist and can I create more?
      • No threading model
      • Single Event Loop
      • Single Event Loop + workers
      • Multiple event loops
    • Programming/Debugging tips
      All threading bugs are caused by access to shared data
      Avoid it where possible
      Make it immutable where possible
    • Deadlocks are usually easy to solve with Memory dumps.
    • Don't run arbitrary unknown code while holding locks. E.g. Don't fire a change notification within the lock.
    • Create attributes that can make classes/members as threadsafe of on the UI thread.

    APP306:WP7 Silverlight DataBinding: How exactly do you do that?

    DEV312:Web Development, the Next Step

    DEV307:Single Line Solutions from the .NET Framework

    Presenter: Bevan Arps

    • INotifyPropertyChanged implementation can be simplified in .NET 4.5 using CallerMemberName attribute
    • Nullable<T> now has .GetValueOrDefault(foo) as an alternative to ??
    • Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) .NET 4.0 [8:00]
    • XPath get xpath evaluation result:
      var namedSubsections = document.XPathEvaluate("section/subsection[@name]"); [18:45]
    • Linq [19:40]
      .TakeWhile discard all that match the condition. Stops as soon as the condition is false.
    • Lazy - Allows for single instantiation and has support for thread safety.
    • ThreadLocal - One instance per thread. [21:30]
    • MoreLINQ - Linq extensions from Jon Skeet
      .MaxBy - Find the highest value.
      .Concat() appends onto the end of the sequence
      .ForEach() for enumerable in addition to the built in support for List.
      .Pipe() applies an action to each item as it passes through the sequence.
    • AsCount - create an extension method on int that allows formatting for singular and plural
    • Exception.AsEnumerable extension method creates a sequence from the root exception and the inner exceptions.
    • AsStrings extension method to extract Data property key value pairs form exceptions. Includes checking for nulls. Every Exception in .NET has a Data property.
    • Chained Null checks
      With - takes an input object and a function
      Return - value or default.

    INO401:The Technology Behind Kinect - The Box of Awesomeness

    Wednesday, September 5, 2012

    APP204 From Web to Windows - Designing Modern Windows 8 Apps using HTML 5, CSS & JavaScript

    Presenter: Gus Pickering, Sam Langley and Zach Hitchcock

    Application Experience Review (AER) was used for early apps. Highly recommended to go through if you can.
    Expression Encoder for screen captures.
    Include a privacy policy and a support email address.
    Typically use 7+ for the age limit unless there is a reason to set it lower.
    No more browser compatibility issues (with JavaScript Windows Apps). If it works for me, it'll work for everyone.
    The Windows UI Design Language
     - Learn the language
     - Put users first - content before chrome
     - Know your content
     - Use the grid wisely
     - Good typography is very powerful
    The Grid
    Fundamental to organise information.
    Many different ways to imply a grid, color, size, proximity, shape,
    Animation enhances comprehension
    Stay Simple, but clever
    Information Architecture
    User Experience
    Make your hit areas bigger
    Snapped view is important
    Include portrait (unless good reasons not to)
    Use semantic zoom
    Use colour, contrast and size
    Windows UI is a horizontal world
    Don't forget about portrait.
    Skeuomorphism - making things look like their real  world counterpart.
    Offers a shallow learning curse for users but you are also modelling the constraints
    Use spatial dimensions not available in the real world.
    But sometimes a button needs to be lickable.
    Media queries make the browser engine do the hard work.
    CSS3 Layout and Typography
    Grid Layout
     display: -ms-grid;
     display: -ms-flexbox;
    Floats done the right way.
    Multi column layout
    JavaScript Libraries
    Will probably contain unnecessary compatibility code.
    Use a second portrait monitor test orientation.
    Learning JavaScript as a programming language
    JavaScript: The Good Parts. Douglas Cockford.
    Avoid: Sams Tech Yourself JavaScript in 24 Hours
    Useful Tools:
    Visual Studio 2012
     - JavaScript Console, DOM Exporer, Simulator
     - Composer - send Get/Put requests to a URL Manually put in values, 
    - AutoResponder - fiddler intercepts and responds with your predefined response
    Javascript libraries
     - Underscore.js - Give functions similar to linq queies in JavaScript
     - Mozilla Developer Reference
    Be disciplined with conventions and testing to avoid issues due to lack of typing system. Write defensive code for type checking.