First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

Verbose XAML, we all love it right?  What?!  You don’t like writing massive amounts of angle brackets to get to define certain properties?  I mean who doesn’t love something like this:


What’s not to love there?  Oh I suppose you prefer something like this?

<MapControl Center="47.669444,-122.123889" />

In the XAML dialect this is what we refer to as a ‘type converter’ or more affectionately at times ‘string to thing’ as the declarative markup is just a string representation of some structure.  In WPF and Silverlight this was implemented through requiring to use the System.ComponentModel.TypeConverter class model where you would attribute your class with a pointer to an implementation of TypeConverter that would override the common things you need, most of the time ConvertFrom capabilities.

In UWP where we currently could not rely on the exact same implementation of System.ComponentModel.TypeConverter as it is not a part of the API exposure to UWP apps at this time as well as being a .NET concept which wouldn’t be available to other WinRT developers.  In looking at ways to achieve the same primary scenario, we can now look at the Creator’s Update to deliver the functionality for us.  In the markup compiler for Creator’s Update we now leverage the metadata CreateFromString attribute in WinRT to generate the correct metdata to do the conversion.  The responsibility lies in the owner of the class (looking at you ISVs as you update) to add this metadata capabilities.

NOTE: To enable this capability, the consuming app must currently have minimum target to the Creator’s Update.

Let’s use an example following my pseudo map control I used above.  Here is my class definition for my MyMap control

using Windows.UI.Xaml.Controls;

namespace CustomControlWithType
    public class MyMap : Control
        public MyMap()
            this.DefaultStyleKey = typeof(MyMap);

        public string MapTitle { get; set; }
        public Location CenterPoint { get; set; }

Notice it has a Location type.  Here’s the definition of that type:

using System; namespace CustomControlWithType { public class Location { public double Latitude { get; set; } public double Longitude { get; set; } public double Altitude { get; set; } } }

Now without a type converter I can’t use the ‘string to thing’ concept in markup…I would have to use verbose markup.  Let’s change that and add an attribute to my Location class, and implement the conversion function:

using System;

namespace CustomControlWithType
    [Windows.Foundation.Metadata.CreateFromString(MethodName = "CustomControlWithType.Location.ConvertToLatLong")]
    public class Location
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Altitude { get; set; }

        public static Location ConvertToLatLong(string rawString)
            string[] coords = rawString.Split(',');
            var position = new Location();
            position.Latitude = Convert.ToDouble(coords[0]);
            position.Longitude = Convert.ToDouble(coords[1]);

            if (coords.Length > 2)
                position.Altitude = Convert.ToDouble(coords[2]);

            return position;

As you can see in the highlighted lines, I added two things.  First I added an attribute to my class to let it know that I have a CreateFromString method and then provided the fully qualified name to that method.  The second obvious thing is to implement that method.  It has to be a public static method and you can see my simple example here.

Now when using the MyMap control I can specify the simpler markup:

And the result would be converted and my control that binds to those values in it’s template are able to see them just fine

Yes, my control is quite lame but just meant to illustrate the point.  The control binds to the CenterPoint.Latitude|Longitude|Altitude properties of the type.

If you are in this scenario of providing APIs that are used in UI markup for UWP apps, try this out and see if it adds delighters for your customers.  I’ve uploaded the full sample of this code to my GitHub in type-converter-sample if you want to see it in full.  Hope this helps! 

2/15/2017 6:14 PM | # re: Implementing a type converter in UWP XAML
Tim is my new most favorite person.
2/16/2017 2:31 AM | # re: Implementing a type converter in UWP XAML
Great post.

Are there any options to control the thrown Exception in the converter? Or XAML does everyhing for us?
2/16/2017 5:09 AM | # re: Implementing a type converter in UWP XAML
Great article.

Can you clarify the point about the need of Creator’s Update? I checked the Microsoft docs and I saw that CreateFromStringAttribute is available since Windows 10 Anniversary Edition, so I thought it was possible with a project targeting Anniversary Update at least.
2/16/2017 8:57 AM | # re: Implementing a type converter in UWP XAML
@Odonno You can compile your class library for 14393, however the app that uses it must set the minimum target to Creators Update, or the app would crash when run on older versions.
8/10/2017 4:25 AM | # re: Implementing a type converter in UWP XAML
Really Great tim, this is really great
8/16/2017 10:23 PM | # free games
Solitaire hearts is best games. It is a mind games. It is help of you growing thinking avility.
Play free hearts online for free. you can play the card game directly out of your computer browser
8/17/2017 1:50 AM | # re: Implementing a type converter in UWP XAML
8/29/2017 7:37 AM | # re: Implementing a type converter in UWP XAML
Really nice i like that your share a great informative article, i was implement this coding and this code in do work but i am face some issue of designing. So i am contact leading company of website deisgn singapore then fix it that issue great stud i am coming again to learn more stuff. Thanks
9/11/2017 5:04 AM | # re: Implementing a type converter in UWP XAML
A terrarium tv apk is one of most rated and top wanted the application for movies and TV shows because of its advanced and amazing features. Download Terrarium TV app in your smart phone and watch all interesting TV shows on regular basis.
9/13/2017 3:32 AM | # re: Implementing a type converter in UWP XAML
I am impressed with the quality of the article in your site! keep up the good work
9/15/2017 11:27 AM | # re: Implementing a type converter in UWP XAML
Really great info! I wish could do a site like this ganar músculo - Enforma10.com
9/21/2017 10:35 PM | # Nice post
Awesome blog. I will bookmark this page for future reference. Thank you so much for sharing. - chennai to shirdi flight package, chennai to shirdi tour package


Please add 4 and 8 and type the answer here:


The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.