C#

Draw the Skeletons in two lines

Hello, Recently I was coding some Kinect stuff and wanted to draw the skeleton that the user can see what’s going on. In the Samples there’s some code for that, but it’s pretty annoying to copy/paste it in your own project every time, so I wrapped it up in a class called SkeletonPainter. To use it you just need an Image of either 640×480 or 320×240 and hand it over to the SkeletonPainter, along with the resolution and a background Brush of your choice: SkeletonPainter painter = new SkeletonPainter(skeletonImage, SkeletonPainterResolution.Resolution320x240, Brushes.White); Of course, you need to hand him the KinectSensor as well: painter.SwitchSensor(sensor); This way you can easily switch the KinectSensor while the painter is still active^ Have a try and tell me what you think =) Download TCD.Kinect.zip

C#

Async CustomMessageBox on Windows Phone

UPDATE: There’s a new Version with .NET4.5 Support, Code Samples Hey there, As you may know, there’s just a limited set of MessageBoxButtons available in Silverlight for WP. However, you can make your own MessageBox in your Silverlight app using the Microsoft.Xna.Framework.GamerServices namespace as explained in this tutorial by Den Delimarsky. This method uses the old style async, which really sucks, so I decided to go the cool and smart new async way. You need Visual Studio Async CTP and Visual Studio 2010 to do this. The trick is to wrap the old async methods together with async Tasks, resulting in a Task<int> that shows the custom message box and returns the selected button in just one line: int result = await CustomMessageBox.ShowAsync(“Dinner time!”, “What do you prefer?”, 0, CustomMessageBoxIcon.Alert, “German”, “Italian”); Asynchronously and therefore non-UI-thread-blocking, of course If you’re interested you can have a look at the source: CustomMessageBox.cs…

C#

Save objects to the ‘LocalFolder’

IMPORTANT: You find the newest versions of TCD.Serialization on NuGet – it works with Windows Phone as well. This post will Show you how to serialize and save objects with XML to your apps LocalFolder/RoamingFolder. Metro style apps in Windows 8 have their own folders where they can store their data. These folders can be accessed by: StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFolder roamingFolder = ApplicationData.Current.RoamingFolder; The difference between these two folders is that Windows 8 will synchronise the RoamingFolder between different machines. This can be useful if you want to improve the user experience, for example by synchronising drafts of a blog post between a users tablet and PC. It’s very important, that the data you want to save is serializable. When designing the data structure and classes for your data, you got to make sure, that each class has a parameterless constructor. Properties can be marked with a set…

C#

Migrating TCD to Windows 8 Release Preview

Two days ago, I migrated my laptop to Windows 8 Release Preview and today Windows Live Mesh finished syncing all of my files. So in the last few hours I migrated all the dev stuff and I’d like to share the latest versions of them too. Most of the libraries haven’t changed that much, only their namespaces have changed from TCD.Windows8.xxxx to TCD.xxxx. TCD.Serialization TCD.Serialization.Xml.XmlDeSerializer – (de)serialize an object from/to a string or Stream using XML TCD.Serialization.Json.JsonDeSerializer – (de)serialize an object from/to a string or Stream using JSON Download TCD.Serialization.zip TCD.Controls TCD.Controls.Flyout – create your own Flyout TCD.Controls.Settings.SettingsContractWrapper – integrate with the settings contract in just a few lines of code Download FlyoutAndSettingsSample.zip

C#

XML and JSON (De)Serialization in WinRT and Windows Phone

In some of my recent Metro/WinRT projects I had to save data to the disk. I decided to go for Xml-serialzation, because it’s human-readable, and flexible. Also it was perfect for the data I wanted to save (eg. metadata about a movie). (If you want to do it on your own: System.IO and System.Xml.Serialization are the namespaces you need for XML (de)serialization.) As I needed this in many projects, I composed a small library, containing a TCD.Serialization.XmlDeSerializer as well as a TCD.Serialization.JsonDeSerializer. Both have four static methods to serialize to/from Stream and string. You can download a package with the sources and a ready-to-use dll. TCD.Serialzation is available on NuGet it works with Windows Phone as well. NOTE: the methods aren’t fail-safe, so you might need to try{}catch{} them to handle exceptions. (For example if you want to serialize to a FileStream without write permission.) Have fun and let me hear…

C#

Performing long-running, timed operations on the UI thread

Recently I had to play timed sounds (morse code) in a WP7 applications. Sounds are required to be played on the UI thread, but as I had to time them, my UI thread got frozen. The reason was, that I used Thread.Sleep() to time the sound, which of course blocked the UI thread. Today I figured out a way to do this long-running operation (many seconds..) without blocking the UI thread. You may know the Visual Studio Async CTP – it’s required to do the trick. Let’s suppose we already have a method InvokeEvent() that plays the sound, changes a color or something like that. Currently we execute this method in a pattern like this, causing the UI thread to be blocked: void StartOperation()//invoke the event in a pattern { for (int i=0; i<20; i++)//20 times { InvokeEvent();//play the sound, blink with the display or something like that Thread.Sleep(500);//this will…

C#

Making a reverse Autocomplete-TextBox on Windows Phone

I’m sure you’re familiar with autocomplete textboxes and maybe you’ve even used them in a project yourself. They provide a fast and easy way for the user to select something, but they lack one important feature: they don’t show what could be selected. ReverseAutocompletePopup is a mixture between an autocomplete text box and the kind of popup you get when a ComboBox has more than five items. At first it displays all options in a ListBox and as you type non-matching ones are filtered out. (And brought back when you delete the text!) EDIT: In the newer version, the user can hit Enter to select, or optionally use his input as the result. When working with it there’re a few things you need to have in mind: The Popup control is no real navigation-thing, so you are in charge to handle OnBackKeyPress according to the guidelines! The Options for the…

C#

Running a synchronous method in an asynchronous context

In WinRT a significant portion of all native methods are asynchronous. Using an asynchronous method is very easy an can help you to speed up you application. The requirement to use an async method is that the calling method has an async modifier. It’s easy to use async methods from a native API, but it can be useful as well to run synchronous operations off the UI thread. This should be done to prevent the UI thread from beeing blocked by time-consuming or resource-intensive operations like calculating ϖ or the answer to the ultimate question for the life, the universe and everything. To show you how to run things like this asynchronously we take the following synchronous method: private void synchonousMethod() { do { i++; } while (i < Int32.MaxValue / 2); } This method obviously lacks the async and await keywords and it’s usual implementation would be something like…

C#

SettingsContractWrapper – the easy way to integrate w/ settings

In the previous post I introduced the Flyout control. Now the most common use case for the Flyout controls is in the inegration with the settings contract. Integrating with the settings contract is usually a two-step procedure. First you have to add a SettingsCommand to the charm and then attach a Flyout or similar to it. Now that you have a Flyout you could do that on your own, but with the SettingsContractWrapper it is even more fun. To use it you  need to define one or more instances of SettingsEntry. This class holds the information required to set up the Flyout Its constructor takes three arguments: Title, FlyoutDimension, Content. The SettingsContractWrapper takes five arguments: Foreground, Background, Theme, Icon, SettingsEntries. The required namespaces are ‘TCD.Controls’ and ‘TCD.Controls.Settings’. As soon as you called the SettingsContractWrapper constructor you can forget about the settings contract integration. Just make sure that the UserControls you hand…