Search code examples
c#xamlxamlreaderstroke

Using XamlReader for controls that does not have a default constructor


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?


Solution

  • It's a "feature" of the XAML language, it is declarative and doesn't know anything about constructors.

    People use ObjectDataProvider in XAML to "translate" and wrap instances of classes that do not have a parameterless constructor (it's also useful for data binding).

    In your case the XAML should look approximately like this:

    <ObjectDataProvider ObjectType="Stroke">
        <ObjectDataProvider.ConstructorParameters>
            <StylusPointsCollection>
                <StylusPoint X="100" Y="100"/>
                <StylusPoint X="200" Y="200"/>
            </StylusPointsCollection>
        </ObjectDataProvider.ConstructorParameters>
    </ObjectDataProvider>
    

    And the code should be:

    var stroke = (Stroke) ((ObjectDataProvider)XamlReader.Parse(xamlStr)).Data;
    

    HTH.