如何动态地将DataTemplate添加到.Xaml文件

时间:2013-10-21 07:38:51

标签: xml-parsing winrt-xaml xamlparseexception

我正在使用T4模板生成datatemplate。这是我正在使用的功能。

public static bool AppendDataTemplate(string filePath, string dataTemplateToBeAppended)
    {
        try
        {
            XmlNode newDataTemplateNode = CreateNodeFromXmlString(dataTemplateToBeAppended);

            if (newDataTemplateNode != null)
            {
                if (newDataTemplateNode.Attributes != null)
                {
                    string itemTemplateKey = newDataTemplateNode.Attributes["x:Key"].Value;

                    XmlElement resourceDictionaryRoot;
                    XmlDocument existingXmlDocument = GetRootDocumentFromFile(filePath, out resourceDictionaryRoot);

                    if (resourceDictionaryRoot != null)
                    {
                        if (
                            resourceDictionaryRoot.ChildNodes.Cast<XmlNode>()
                                .Any(
                                    node =>
                                    node.Attributes != null && node.Attributes["x:Key"].Value == itemTemplateKey))
                        {
                            return true;
                        }

                        if (resourceDictionaryRoot.OwnerDocument != null)
                        {
                            XmlNode importNode = resourceDictionaryRoot.OwnerDocument.ImportNode(
                                newDataTemplateNode, 
                                true);
                            resourceDictionaryRoot.AppendChild(importNode);
                        }
                    }
                    else
                    {
                        throw new ApplicationException(
                            "There is some issue while getting xml from the specified file");
                    }

                    existingXmlDocument.Save(filePath);
                }
            }
            else
            {
                throw new ApplicationException("There is some issue while converting specified string to XML");
            }
        }
        catch (Exception ex)
        {
            throw;
        }

        return true;
    }
private static XmlNode CreateNodeFromXmlString(string xml)
    {
        var newDataTemplateDocument = new XmlDocument();
        newDataTemplateDocument.XmlResolver = null;
        newDataTemplateDocument.LoadXml(xml);
        return newDataTemplateDocument.DocumentElement;
    }

这个工作正常,但唯一的问题是,我不得不添加xml命名空间和datatemplate,否则它会抛出错误,反正我们可以停止XML验证吗?看下面的模板,如果我没有xmlns传递它会抛出验证错误..有什么建议吗?

<DataTemplate x:Key="PersonItemTemplate" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid HorizontalAlignment="Left" Width="250" Height="250">
        <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
            <Image Source="{Binding ImageUrl}" Stretch="UniformToFill" />
        </Border>
        <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
            <TextBlock Text="{Binding PersonName}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="40" Margin="15,0,15,0" />
            <TextBlock Text="{Binding PersonId}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" Margin="15,0,15,10" />
            <TextBlock Text="{Binding PersonAddress}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" Margin="15,0,15,10" />
            <TextBlock Text="{Binding BirthDate}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" Margin="15,0,15,10" />
        </StackPanel>
    </Grid>
</DataTemplate>

0 个答案:

没有答案