I have some string representations of Xaml objects, and I want to build the controls. I'm using the XamlReader.Parse function to do this. For a simple control such as Button that has a default constructor not taking any parameters this works fine:
var buttonStr = "<Button xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">Text</Button>";
var button = (Button)XamlReader.Parse(buttonStr);
However, when I try to do this to e.g. a Stroke control it fails. First trying a simple empty Stroke:
var strokeStr = "<Stroke xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"></Stroke>";
var stroke = (Stroke)XamlReader.Parse(strokeStr);
This gives the error:
Cannot create object of type 'System.Windows.Ink.Stroke'. CreateInstance failed, which can be caused by not having a public default constructor for 'System.Windows.Ink.Stroke'.
In the definition of Stroke I see that it needs at least a StylusPointsCollection to be constructed. I assume this is what the error is telling me, though was kinda assuming this would be handled by the XamlReader. Trying to transform a Xaml of Stroke with StylusPoints in it gives the same error:
var strokeStr =
"<Stroke xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
"<Stroke.StylusPoints>" +
"<StylusPoint X=\"100\" Y=\"100\" />" +
"<StylusPoint X=\"200\" Y=\"200\" />" +
"</Stroke.StylusPoints>" +
"</Stroke>";
var stroke = (Stroke) XamlReader.Parse(strokeStr);
What am I doing wrong? How do I tell the XamlReader how to create the Stroke correctly?