Ошибка Silverlight в коде DataTemplate NameSpace - PullRequest
0 голосов
/ 11 августа 2010

Когда я создаю шаблон данных в коде, я получаю сообщение об ошибке при определении пространств имен.

Ниже приведен код, который я использую

 public static DataTemplate CreateDataTemplate(string bindingValue)
    {
        StringBuilder dataTemplateText = new StringBuilder();           
        dataTemplateText.Append("<DataTemplate ");
        dataTemplateText.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
        dataTemplateText.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
        dataTemplateText.Append("xmlns:interactivity='http://schemas.microsoft.com/expression/2010/interactivity' ");

        // Custom NameSpace
        dataTemplateText.Append("xmlns:valueConverter='clr-namespace:AttachedPropertiesTest.ValueConverter;assembly=AttachedPropertiesTest'> ");
        dataTemplateText.Append("xmlns:wtf='clr-namespace:AttachedPropertiesTest.wtf;assembly=AttachedPropertiesTest'> ");
        //

        dataTemplateText.Append("<Grid>");
        dataTemplateText.Append("<Grid.Resources>");
        dataTemplateText.Append("<valueConverter:RowIndexConverter x:Key='rowIndexConverter' />");
        dataTemplateText.Append("</Grid.Resources>");
        dataTemplateText.Append("<TextBlock ");
        dataTemplateText.Append("Text = '{Binding  ");
        dataTemplateText.Append("Converter={StaticResource rowIndexConverter}, ConverterParameter=" + bindingValue + " }' >");
        dataTemplateText.Append(" <interactivity:Interaction.Triggers> ");
        dataTemplateText.Append(" <interactivity:EventTrigger EventName='MouseLeftButtonDown' > ");
        dataTemplateText.Append("      <wtf:InvokeDelegateCommandAction CommandName='CellCommand' Command='{Binding CellCommand, Source={StaticResource mainViewModel}}'  CommandParameter='{Binding Converter={StaticResource rowIndexConverter}, ConverterParameter=" + bindingValue + " }' />  ");
        dataTemplateText.Append(" </interactivity:EventTrigger> ");
        dataTemplateText.Append(" </interactivity:Interaction.Triggers> ");
        dataTemplateText.Append("</TextBlock>");
        dataTemplateText.Append("</Grid>");
        dataTemplateText.Append("</DataTemplate>");

        DataTemplate dataTemplate = (DataTemplate)XamlReader.Load(dataTemplateText.ToString());
        return dataTemplate;
    }

Я получаю следующую ошибку:

Не удалось создать 'System.Windows.DataTemplate' из текста 'xmlns: wtf =' clr-namespace: AttachedPropertiesTest.wtf; assembly = AttachedPropertiesTest '>'.

Когда у меня есть только одно пространство имен, где я устанавливаю сборку, она работает.

Я могу заставить свой код работать, поместив весь мой код в одно определение пространства имен.Я просто не фанат этой работы вокруг.

Кто-нибудь знает, что я делаю неправильно, определяя мои пространства имен.

Ответы [ 2 ]

1 голос
/ 11 августа 2010

Первое пользовательское пространство имен содержит закрывающую скобку, которой там быть не должно.

dataTemplateText.Append ("xmlns: valueConverter = 'clr-namespace: AttachedPropertiesTest.ValueConverter; assembly = AttachedPropertiesTest'>");

Вероятно, должно быть:

dataTemplateText.Append ("xmlns: valueConverter = 'clr-namespace: AttachedPropertiesTest.ValueConverter; assembly = AttachedPropertiesTest'");

0 голосов
/ 11 августа 2010

Вы можете попробовать объявить ваши строки следующим образом:


dataTemplateText = @"
<DataTemplate
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:interactivity='http://schemas.microsoft.com/expression/2010/interactivity'
{more here...}
";

, чтобы значения были более читабельными.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...