[英]Silverlight In Code DataTemplate NameSpace Error
当我在代码中创建DataTemplate时,定义名称空间时出现错误。
下面是我正在使用的代码
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;
}
我收到以下错误:
无法从文本'xmlns:wtf ='clr-namespace:AttachedPropertiesTest.wtf; assembly = AttachedPropertiesTest'>'创建'System.Windows.DataTemplate'。
当我只有一个设置程序集的名称空间时,它将起作用。
通过将所有代码放在一个命名空间定义中,可以使代码正常工作。 我只是不喜欢这项工作。
有谁知道通过定义我的命名空间我在做什么错。
第一个自定义名称空间包含一个不应包含的右括号。
dataTemplateText.Append(“ xmlns:valueConverter ='clr-namespace:AttachedPropertiesTest.ValueConverter; assembly = AttachedPropertiesTest'>”);
应该可能是:
dataTemplateText.Append(“ xmlns:valueConverter ='clr-namespace:AttachedPropertiesTest.ValueConverter; assembly = AttachedPropertiesTest'”);;
您可以尝试这样声明您的字符串:
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...}
";
因此这些值更具可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.