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.
3/10/2017 1:03 AM | # re: Implementing a type converter in UWP XAML
very nice post
3/15/2017 1:48 AM | # re: Implementing a type converter in UWP XAML
Very Nice Post

I know some places to read really high quality crisply written articles. I am sure you’ll love some of these.

For More Information , Read This http://www.commercialcleaninginmelbourne.net.au

Cleaning products basically contain many chemical substances to clean efficiently. These substances and toxins can harm the people, animals and natural environment as well. Most of the janitorial services have now started to provide environment friendly services. In such an attempt they use a variety of organic and biodegradable cleaning products. Getting the help of janitorial services help your employees to realize that you are creating a work environment that is safe for them. You can hire Office Cleaning Services for your office that uses environment friendly material. Pop over to this web-site http://www.commercialcleaninginmelbourne.net.au/ for more information on Office Cleaning Services.

Commercial Cleaning In Melbourne

15 Eastgate Rd, Craigieburn VIC 3064, Australia
3/16/2017 2:38 AM | # re: Implementing a type converter in UWP XAML

Fixed Wireless Internet providers can provide users access to the World Wide Web. It also assures quick transfer and exchange of data. There are some steps you can take so that you can choose an excellent wireless internet provider for yourself. If you want a good wireless internet provider, then you need to put a bit of time and effort into it. Browse this site www.tierzero.com/.../fixed-wireless-internet/ for more information on Fixed Wireless Internet.
Follows US: https://goo.gl/xKAgko
3/24/2017 3:17 AM | # Reach Forklift Rental Los Angeles
It decreases the efforts of the employees, as they do not have to lug hefty lots by hand. Reach Forklift Rental Los Angeles is devices used for elevating persons or challenges the required elevation. This form of lift relocates just backwards and forwards. It is thoroughly used in manufacturing as well as layout market where it is common for individuals to work in challenging to reach rooms. It is likewise made use of for doing upkeep work of high-rise buildings. Look at this web-site westcoastequipment.us/reach-forklift-rentals/ for more information on Reach Forklift Rental Los Angeles.
Follow us:https://goo.gl/GvRFyN
3/25/2017 8:09 AM | # re: Implementing a type converter in UWP XAML
3/27/2017 5:48 AM | #  Construction Equipment Rental Los Angeles CA

Appreciating the time and effort you put into your website and in depth information you offer. It's great to come across a blog every once in a while that isn't the same unwanted rehashed information. Great read! I've saved your site and I'm adding your RSS feeds to my Google account.

Here is my web page: http://westcoastequipment.us

Construction Equipment Rental Los Angeles CA

Please add 6 and 4 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.