Category Archives: C# Snippets

WP7 Navigation Gotcha

Navigating to a page, passing a key defined by a third party can present problems, but taking a little care over the way you pass the parameters can save a world of pain later. Continue reading

Posted in .NET, C# Snippets, Windows Phone, WP7 | Leave a comment

Silverlight Toolkit ExpanderView – Flat Objects

Just one of many ways in which you can bind an ExpanderView control to a collection of simple objects. Continue reading

Posted in .NET, C# Snippets, Windows Phone | Leave a comment

Alter a Pivot controls header template the easy way (WP7)

If you want to alter the template for a Windows Phone 7 pivot control’s header, the simplest way is as follows… Of course, all this example does is specifically set the colour used for the title and headers to black, … Continue reading

Posted in .NET, C# Snippets, Mango, Windows Phone, WP7 | Leave a comment

Alternating ListBox item background colours in WP7

I had a requirement today to implement alternating row colours in a Windows Phone 7 ListBox. After a bit of frustration with searching and only finding answers tht said “You can’t do it”, or that you need to add a … Continue reading

Posted in .NET, C# Snippets, Mango, Windows Phone, WP7 | 1 Comment

Using LINQ to randomise a list.

This afternoon I needed to select some random items from a collection, and my searching ran into this idea. I modified it a little to do what I needed (Select 4 items at random from a list), and this is … Continue reading

Posted in C# Snippets, Linq | Leave a comment

Split a Camel-Cased word into its components

I needed to split a camel-cased word into it’s component parts, and found the following extension method to help out. It works pretty well.

Posted in .NET, C# Snippets, Extension Methods, Uncategorized | Leave a comment

Apostrophe In RowFilter

According to ASP.NET Resources, there is a problem when using apostrophes in a RowFilter expression in a DataView. The fix seems to be to double the apostrophes (“O”Mally”) rather than escaping them (“O\’Mally”)

Posted in C# Snippets | Comments Off

Iterate through enum values

A quick example of how to iterate through the values in an enum… enum myEnum { one, two, three, four, five, }; foreach (int i in Enum.GetValues(typeof(myEnum ))) { myEnum myItem = (myEnum )Enum.ToObject(typeof(myEnum ), i); System.Diagnostics.Debug.WriteLine(myItem.ToString()); }

Posted in .NET, C# Snippets, General, Software | Comments Off

Determine if an assembly is a debug or release build

*This code originally came from another blog, although it was written in VB over there. It has lost some elegance in the translation, but I needed a very quick solution this afternoon, and I needed it in C#, so here … Continue reading

Posted in .NET, C# Snippets, Software | 1 Comment

Sending an HTTP POST request to a web server

ASCIIEncoding encoding = new ASCIIEncoding(); string post_url = “http://172.16.24.44/postdata.php”; string poststring = “monkeys=blue\&trousers=black”; byte[] data = encoding.GetBytes(poststring); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(post_url); httpRequest.Method = “POST”; httpRequest.ContentType=”application/x-www-form-urlencoded”; httpRequest.ContentLength = data.Length; Stream newStream=httpRequest.GetRequestStream(); newStream.Write(data,0,data.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse (); Stream receiveStream = … Continue reading

Posted in C# Snippets | Comments Off