DateTime projection in C++/CX

C# projects Windows::Foundation::DateTime as System.DateTimeOffset, whereas C++ just exposes the raw structure. This can be rather unnerving if you are trying to port some C# code to C++/CX. All you have to work with is the UniversalTime property which is a long long that MSDN defines as the number of 100-nanosecond intervals prior to or after midnight on January 1, 1601. Even a simple task as displaying a formatted date/time string is difficult as ToString merely returns the fully qualified name of the type.

Fortunately enough, Windows::Globalization::DateTimeFormatting includes the DateTimeFormatter class that can be used to get readable date/time display strings from a DateTime structure. Here’s some sample code that shows how you can use this.

auto dateFormatter = DateTimeFormatter::LongDate;
auto timeFormatter = DateTimeFormatter::LongTime;

for(auto item : feed->Items)
    dateFormatter->Format(item->PublishedDate) + " " +
    timeFormatter->Format(item->PublishedDate) + " - " +  

Obviously, it’d have been heaps better if the compiler gave us easier to use projections, but it’s still not that bad. A common practice is to convert this to standard C++ or ATL/Win32 date structures and formats if you want to do additional date/time processing on the object.


Compiler errors when using the Bindable attribute

It’s been a long break for me from blogging, but I intend to make up for that over the next few months. I’ll primarily be blogging on topics mostly related to using Visual C++ to develop Windows Store applications. So if that’s the sort of thing that interests you, do check back once in a while.

Putting the BindableAttribute on a C++ ref class makes the class available for databinding. Unfortunately, if you add a very simple ref class and then mark it as [Bindable], you’ll get some weird compiler error messages. Example – consider this simple bindable class defined in Restaurant.h.

namespace ViewModels
  public ref class Restaurant sealed

Assume there’s a Restaurant.cpp that includes this file. You’ll get these compiler errors (or something close to it).

Error 1 error C3083: 'ViewModels': the symbol to the left 
    of a '::' must be a type ...\xamltypeinfo.g.cpp 
Error 2 error C2039: 'Restaurant' : is not a member 
    of 'ViewModels' ...\xamltypeinfo.g.cpp 
Error 3 error C2061: syntax error : identifier 
    'Restaurant' ...\xamltypeinfo.g.cpp

The not so obvious fix for these errors is to include Restaurant.h in any of your xxx.xaml.h files (right after the include to the xxx.g.h file would be a good place). This seems to be due to how the XAML compiler ties into the whole build process. Not a major hassle, but more something to be aware of.