C#

A Custom (Async) MessageBox for WPF and Windows Phone

The System.Windows.MessageBox provided by .NET is limited to a very small set of MessageBoxButtons (Yes/No/OK/Cancel) which do not allow for custom Button texts. The System.Windows.Forms.MessageBox also blocks the UI thread, which can be an annoying disadvantage as well. The MessageBox on Windows Phone has the same disadvantages. Custom buttons in a MessageBoxe simplify the decision for the user, because they can describe the results of the action: However you need to do this with a custom control, so for WPF as well as Windows Phone I designed a simple method for asynchronously showing a MessageBox with custom buttons. (The WPF implementation based on this control by Evan Wondrasek) So let’s bring some sample code.In WPF you can use multiple custom buttons: And on Windows Phone you’re limited to 2 buttons: To use CustomMessageBox, you need to get TCD.Controls from NuGet: And on Windows Phone 7.x you also need async support,…

C#

TCD.Web.NotiConn – communicate between handsets without a server

Two days ago, when I was cycling, suddenly I had the answer: Imagine you have two Windows Phone 7 devices and want to establish a two-way connection between them. For example to play a game of Tic-Tac-Toe. You can do this either with a server on the internet, that connects the clients with each other, or the multicast protocol on WiFi-networks. The same WiFi network may not always be available to both users, but you don’t want to set up a server either (maybe, because you’re only doing this for fun, or you don’t have the resources). The only way to send messages to a WP7 app via the internet is to use push notifications. But how do you exchange the channel addresses between the devices? One way is to use a QR code: Establish a push notification channel on both devices Encode the channel URI into a QR code…

C#

TCD.Controls

This is a summary and manual for all controls inside the TCD.Controls package. I’ll try to keep it updated. Windows 8 SilentTextBox – If AcceptsReturn=False and the user hits Enter normal TextBoxes make a ‘bling’ sound. SilentTextBox does not. HeaderedTextBox – A TextBox with a grey header above HeaderedTextBlock – A TextBlock with a grey header above; Automatically hides itself if the Text property is empty (unless AutoHide is set to False). LabeledProgressBar – a ProgressBar coupled with a TextBlock/Label; use it at the top of your page to indicate background progress (like the SystemTray.ProgressBar on Windows Phone) LayoutAwarePage – same as in all the samples Flyout – most recent post and how to use on Flyout SettingsContractWrapper – the easy way to integrate with the settings contract   Windows Phone CustomMessageBox – Async CustomMessageBox on Windows Phone ReverseAutocompletePopup – Making a reverse Autocomplete-TextBox on Windows Phone

C#

TCD.Device

Just another TCD namespace: TCD.Device. Its purpose is to contain additional namespaces that use features like Camera, GPS, Accelerometer, Compass.. Everything that has an effect on the App permissions the user sees on the Windows Phone or Windows 8 application. (Like ‘This app has access to your location, compass..’) TCD.Device.Camera CodeScannerPopup is another async control, specific to the Windows Phone platform, that can be used to scan for QR codes. (In theory other 2D-codes as well.) Usage: add the TCD.Device.Camera NuGet package add using directives for TCD.Device.Camera and TCD.Device.Camera.Barcodes in the calling method (eg. a Button.Click event) hide/show ApplicationBar, attach/remove BackKeyPress event Code sample: private async void Button_Click(object sender, RoutedEventArgs e) { CodeScannerPopup scp = new CodeScannerPopup(Microsoft.Phone.Shell.SystemTray.IsVisible, Format.ALL_1D); //important: hide ApplicationBar and handle BackKeyPress ApplicationBar.IsVisible = false; this.BackKeyPress += scp.HandleBackKeyPress; //now let’s go ScanResult r = await scp.ShowAsync(“BARCODE SCANNER”, “scan”); if (r != null) output.Text = string.Format(“{0}n{1}”, r.Text, r.Format.ToString()); //clean…

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#

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…