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#

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…

C#

Flyout control for Windows 8 Metro (XAML/C#)

IMPORTANT: This post ist partly outdated. TCD.Controls is now available on NuGet, so you don’t have to import the *.xaml into your project anymore! All code shown in this post still applies. Please refer to this post for more information on the changes. In Windows 8 Metro style apps there’s something called ‘Flyouts’. It’s a panel that slides in from the right. Sadly the control is not available to C#/VB developers. In this post Tim Heuer shows how to integrate with the settings contract using flyouts. Based on his code I developed a Flyout control for XAML/C# solutions. Here’s a list of what it can or can’t do: It does: light dismissal, back button easy integration of ‘content’ swipe-in transition custom color theme ‘narrow’ and ‘wide’ mode It does not: theming of the BackButton beeing attached to the left construct it in XAML I would like to provide you with a dll, but there’s a…

Root

WikiSearch – a quick and dirty Windows 8 Metro App

I’ve been playing around with Windows 8 Metro development the last months and one day I did a quick Wikipedia App that integrates into the Search. The first version took me – no joke – less than 20 minutes, cause it’s just so easy! All you have to do to integrate into the search is to add this method to your App.xaml.cs: protected override void OnSearchActivated(SearchActivatedEventArgs args) { base.OnSearchActivated(args); if (mainPage == null) mainPage = new MainPage(); Window.Current.Content = mainPage; mainPage.OpenWiki(args.QueryText); Window.Current.Activate(); } After that go into Package.appxmanifest, switch to the Declarations tab and add a ‘Search’ declaration. mainPage.OpenWiki() would be a method that composes a Wikipedia-Search-URL from the queryText and tells a WebView to navigate to it. (webView.Source = new Uri(“http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search=” + “queryString”);) I won’t go into any more detail at this point, but that is all the magic you have to do. Today I added multiple-language support to…