Search code examples
c#xamlwindows-phone-8datatemplatedatatemplateselector

Creation of DataTemplate in code-behind fails


I'm trying to create a DataTemplate in my code and I came across this asnwer.

So I just copied and edited the code, but it fails with this exception:

First-chance exception 'System.Windows.Markup.XamlParseException' in System.Windows.ni.dll Unknown parser error: Scanner 2147500037. [Line: 4 Position: 36]

Here's the generated XAML code:

<DataTemplate
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:simplebackground="clr-namespace:Plugins.Backgrounds.SimpleBackground">
    <simplebackground:SimpleBackground/>
</DataTemplate>

and here's the XAML code that I'm currently using in my page (this one's working):

<phone:PhoneApplicationPage
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:simpleBackground="clr-namespace:Namespace.Backgrounds.SimpleBackground"
    x:Class="Namespace.Backgrounds.SimpleBackground.SimpleBackground" mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="480" d:DesignWidth="480">

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="DataTemplate">
            <simpleBackground:SimpleBackground />
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>
  .............
<phone:PhoneApplicationPage>

To generate the XAML I'm using this C# code:

public static DataTemplate Create(Type type)
{
    var templateString = "<DataTemplate\r\n" +
                         "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\r\n" +
                         "xmlns:" + type.Name.ToLowerInvariant() + "=\"clr-namespace:" + type.Namespace + "\">\r\n" +
                         "<" + type.Name.ToLowerInvariant() + ":" + type.Name + "/>\r\n" +
                         "</DataTemplate>";            
    return XamlReader.Load(templateString) as DataTemplate;
}

What's wrong with it? The exception's message is not that useful :(


Solution

  • The templateString in Create contains an element the XamlReader cannot find. You'll have to add the assembly in which the element resides to the namespace:

    public static DataTemplate Create(Type type)
    {
        var templateString = 
            "<DataTemplate " +
                "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +                                   
                "xmlns:" + type.Name.ToLowerInvariant() +
                     "=\"clr-namespace:" + type.Namespace +
                     ";assembly=" + type.Assembly.GetName().Name + "\">" +
            "<" + type.Name.ToLowerInvariant() + ":" + type.Name + "/>" +
            "</DataTemplate>";            
        return XamlReader.Load(templateString) as DataTemplate;
    }