SharpDX.SimpleInitializer, an easy way of initializing SharpDX in your XAML projects

After working on some Windows Phone apps that required the usage of a DrawingSurface/DrawingSurfaceBackgroundGrid to perform Direct3D drawing, I have isolated some of the code in a standalone library for easily initialization of the Direct3D device, context and associated resources.

The library abstracts the initialization of Direct3D and transparently handles such cases as the control being resized (for example, a Windows Store application being snapped) and the device being lost or recreated (Windows Phone does this when sending the application to the foreground). The only dependency comes from SharpDX and it can be used in Windows Phone 8, Windows Phone 8.1 Silverlight, Windows Store 8.1 and Universal projects.

This is the workflow you should follow when using it:

  • When navigating to a page where the Direct3D drawing will be performed, instantiate a SharpDXContext object.
  • Subscribe to its DeviceReset and Render events to perform resource loading and drawing, respectively.
  • Call BindToControl passing the existing DrawingSurface, DrawingSurfaceBackgroundGrid, SwapChainPanel or SwapChainBackgroundPanel control.
  • The event Render will be raised whenever the control asks for a new frame, perform your drawing there using the D3DDevice and D3DContext properties.
  • When navigating away from the page, unsubscribe from the events and Dispose the SharpDXContext object.

You can download the library from NuGet, or take a look at the code on GitHub. Some sample projects that show how to properly manage the lifetime of the SharpDXContext object are available here.

Problems connecting a Windows Phone device to an ASUS P7P55D PRO motherboard

I have had some problems when connecting Windows Phone devices to my desktop computer, from when it had Windows 7 installed and my phone was an HTC Trophy to right now, with Windows 8.1 and a Lumia 920. Other USB devices had no problem synchronizing but connecting a Windows Phone almost always resulted in weird behaviour:

  • The notification alert for device connected/disconnected played, but the device didn’t appear anywhere, not even Device Manager.
  • Device appeared, but when opening any window that had to list it, explorer.exe hanged.
  • Device appeared and everything seemed OK, but opening the Windows Phone application failed to connect with the “Your phone is having troubles” message. Trying to deploy an application from Visual Studio failed with the error “This operation returned because the timeout period expired“.

The solution is very simple: go ahead and flash the latest BIOS version available (2101, released on 06/11/2012). You can use the integrated EZ Flash 2 tool from the BIOS menu to do it easily, because it can read the ROM file from a plugged USB device or from the computer’s disk, even if it is formatted as NTFS.

My motherboard had the stock version (0501, from 26/08/2009), and I suspect that the version that fixed the problem is 0915 since it lists “Improve compatibility with some USB device” in the changelog.

Extension methods for switching BinaryReader/BinaryWriter endianness

Although very simple, I have published a new small portable class library that adds some extension methods to the BinaryReader and BinaryWriter classes. It consists of overloads to the Read* and Write methods to allow switching the endianness of the data being readen/written. Since almost all current .NET CLR platforms run on little endian (Windows Phone does too, thanks to the ability of ARM to switch between big and little endian), these functions are helpful when reading data exported from other platforms, or more commonly, JPEG files.

The library is available for download from GitHub, both in source code and compiled form. Or if you prefer, you can search for the BinaryEndiannessExtensions package on NuGet and add it directly to your project.