Need to convert a TimeSpan to a sensible string for you Windows Phone or Windows Store App. With a little Bing help I created this converter. I hope you find it useful.
public class TimeSpanToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
TimeSpan span = (TimeSpan)value;
if (span == TimeSpan.MinValue)
{
return "0 seconds";
}
string formatted = string.Format("{0}{1}{2}{3}",
span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);
if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);
if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";
return formatted;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
string strValue = value as string;
TimeSpan resultSpan;
if (TimeSpan.TryParse(strValue, out resultSpan))
{
return resultSpan;
}
throw new Exception("Unable to convert string to date time");
}
}
If like me you didn’t understand the the rest of the message and it’s implications here is a simple explanation and answer.
When an object that implements an inherited class (e.g. a ViewModel that inherits from ViewModelBase) is constructed, the constructors in the base class or classes are executed first.
However as the base classes execute methods or events, if these implemented methods or events are overridden, by a more derived class, then the base could call these and not it’s intended base methods. This could cause some unexpected behaviour.
If you make calls to potentially override-able methods yourself (e.g. RaisePropertyChanged) , you probably intended to and know full well what the consequences are. So why the annoying message?
Well, CA2214 is warning you that this may become a problem if anyone decides to inherit from your class (e.g. your ViewModel). As this is probably not something that you want or intend in your project, you can eliminate this warning by simply sealing your class!
public sealed class MainViewModel : ViewModelBase
This will ensure that your class cannot be inherited from. This should make the warning go away.
If you are writing your own base class or a class that is to be inherited from, then don’t call override-able methods in your constructors!
The LongListSelector is great for long lists in Windows Phone 7, but getting your data into the right format can be tricky and confusing (certainly took me a while to work out what to do!). Especially if you want to use ‘pure’ binding to an ObservableCollection.
The answer that I found easiest to understand and follow was to use ‘nested Observable collections’. This also ensure that things are kept up-to-date in the UI without additional coding.
To help me understand what was need I broke it down into some simple stages. I am sure it can be done with a single LINQ statement.
Let say you have a list of contacts that you would like to display with the typical A-Z short cuts as shown here. This includes showing group titles that contain no child records too!
Create the basic object – Contacts
We’ll start with the simple Contact class that has two 3 properties. One is derived to create the key value. This can be any value, but here it’s the first letter of the Name value in lower case. If your data is changing then you should implement the INotifyPropertyChanged interface. I have left it out here for simplicity.
namespace PhoneTester1.Model
{
public class Contact
{
public string Name { get; set; }
public string JobTitle { get; set; }
private string _NameKey;
/// <summary>
/// First letter of the Name. Used for grouping in the long list selector
/// </summary>
public string NameKey
{
get
{
//If not set then
if (_NameKey == null)
{
//get the first letter of the Name
char key = char.ToLower(this.Name[0]);
if (key < 'a' || key > 'z')
{
key = '#';
}
_NameKey = key.ToString();
}
return _NameKey;
}
}
}
}
Create the Group Observable Collection
Then we’ll create a Group class. I have called it GroupOC for Grouping Observable Collections.
using System.Collections.ObjectModel;
namespace PhoneTester1.Model
{
public class GroupedOC<T>:ObservableCollection<T>
{
/// <summary>
/// The Group Title
/// </summary>
public string Title
{
get;
set;
}
/// <summary>
/// Constructor ensure that a Group Title is included
/// </summary>
/// <param name="name">string to be used as the Group Title</param>
public GroupedOC(string name)
{
this.Title = name;
}
/// <summary>
/// Returns true if the group has a count more than zero
/// </summary>
public bool HasItems
{
get
{
return (Count != 0);
}
private set
{
}
}
}
}
Create the Helper method to Group the Contacts
I created a static method called CreateGroupedOC in a static Collection Helper class called CollectionHelper. This can then be called on to group everything up.
using System.Collections.ObjectModel;
using System.Linq;
using PhoneTester1.Model;
namespace PhoneTester1.Functions
{
public static class CollectionHelpers
{
/// <summary>
/// Groups a passed Contacts ObservableCollection
/// </summary>
/// <param name="InitialContactsList">Unordered collection of Contacts</param>
/// <returns>Grouped Observable Collection of Contacts suitable for the LongListSelector</returns>
public static ObservableCollection<GroupedOC<Contact>> CreateGroupedOC(ObservableCollection<Contact> InitialContactsList)
{
//Initialise the Grouped OC to populate and return
ObservableCollection<GroupedOC<Contact>> GroupedContacts = new ObservableCollection<GroupedOC<Contact>>();
//first sort our contacts collection into a temp List using LINQ
var SortedList = (from con in InitialContactsList
orderby con.Name
select con).ToList();
//Now enumerate throw the alphabet creating empty groups objects
//This ensure that the whole alphabet exists even if we never populate them
string Alpha = "#abcdefghijklmnopqrstuvwxyz";
foreach (char c in Alpha)
{
//Create GroupedOC for given letter
GroupedOC<Contact> thisGOC = new GroupedOC<Contact>(c.ToString());
//Create a temp list with the appropriate Contacts that have this NameKey
var SubsetOfCons = (from con in SortedList
where con.NameKey == c.ToString()
select con).ToList<Contact>();
//Populate the GroupedOC
foreach (Contact csm in SubsetOfCons)
{
thisGOC.Add(csm);
}
//Add this GroupedOC to the observable collection that is being returned
// and the LongListSelector can be bound to.
GroupedContacts.Add(thisGOC);
}
return GroupedContacts;
}
}
}
Tie it together in your ViewModel
In my projects I am usually populating a collection of objects either from a database or web service. Here we’ll start with a collection of Contacts that is populated in a random order. This ObservableCollection we would pass to the method shown above (CreateGroupedOC ) to group it and return a grouped ObservableCollection.. This can then be referenced in your ViewModel and bound to the LongListSelector control.
I have included my whole ViewModel below. Hopefully the comments make it self explanatory!
using System;
using System.Collections.ObjectModel;
using System.Text;
using GalaSoft.MvvmLight;
using PhoneTester1.Functions;
using PhoneTester1.Model;
namespace PhoneTester1.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// You can also use Blend to data bind with the tool's support.
/// This code is provided as is by Hard Medium Soft Ltd.
/// see us at http://www.hardmediumsoft.com
/// </summary>
public class MainViewModel : ViewModelBase
{
public string ApplicationTitle
{
get
{
return "Nicholas Rogoff for Hard Medium Soft Ltd.";
}
}
public string PageName
{
get
{
return "Tester App";
}
}
/// <summary>
/// The <see cref="GroupedContacts" /> property's name.
/// </summary>
public const string GroupedContactsPropertyName = "GroupedContacts";
private ObservableCollection<GroupedOC<Contact>> _GroupedContacts = new ObservableCollection<GroupedOC<Contact>>();
/// <summary>
/// Gets the GroupedContacts property.
///
/// Changes to that property's value raise the PropertyChanged event.
/// This property's value is broadcasted by the Messenger's default instance when it changes.
/// </summary>
public ObservableCollection<GroupedOC<Contact>> GroupedContacts
{
get
{
return _GroupedContacts;
}
set
{
if (_GroupedContacts == value)
{
return;
}
var oldValue = _GroupedContacts;
_GroupedContacts = value;
// Update bindings, no broadcast
RaisePropertyChanged(GroupedContactsPropertyName);
}
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
//Populate some Contacts with random strings
ObservableCollection<Contact> tempContactsOC = new ObservableCollection<Contact>();
for (int i = 0; i < 50; i++)
{
Contact tempContact=new Contact(){Name = RandomString(12,false,i) + " - " + i, JobTitle=RandomString(18,false,i)};
tempContactsOC.Add(tempContact);
}
//Create our Nested ObservableCollection
GroupedContacts = CollectionHelpers.CreateGroupedOC(tempContactsOC);
}
#region Random String generator
/// <summary>
/// Generates a random string with the given length
/// </summary>
/// <param name="size">Size of the string</param>
/// <param name="lowerCase">If true, generate lowercase string</param>
/// <returns>Random string</returns>
private string RandomString(int size, bool lowerCase, int seed)
{
StringBuilder builder = new StringBuilder();
Random random = new Random((int)DateTime.Now.Ticks * seed);
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
#endregion
}
}
If you think you can make this simpler or better please let me know. Thanks.