【问题标题】:Creation of DataTemplate in code-behind fails在代码隐藏中创建 DataTemplate 失败
【发布时间】:2014-01-09 09:14:38
【问题描述】:

我试图在我的代码中创建一个DataTemplate,但遇到了this asnwer

所以我只是复制和编辑了代码,但它失败了这个异常:

System.Windows.ni.dll 中的第一次机会异常“System.Windows.Markup.XamlParseException” 未知的解析器错误:扫描仪 2147500037。[行:4 位置:36]

这是生成的 XAML 代码:

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

这是我目前在我的页面中使用的 XAML 代码(这个正在工作):

<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>

要生成 XAML,我正在使用此 C# 代码:

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;
}

它有什么问题? 异常的消息没有那么有用:(

【问题讨论】:

    标签: c# xaml windows-phone-8 datatemplate datatemplateselector


    【解决方案1】:

    Create 中的templateString 包含XamlReader 找不到的元素。您必须将元素所在的程序集添加到命名空间:

    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;
    }
    

    【讨论】:

    • 我会试一试,但我在同一个程序集中创建此代码,其中我有我要创建的元素!
    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多