动态添加时无法创建未知类型

时间:2018-07-12 10:05:25

标签: c# wpf xaml parsing dynamically-generated

错误:

  

System.Windows.Markup.XamlParseException:``无法创建未知类型   “ {schemas.xceed.com/wpf/xaml/toolkit}DoubleUpDown”。 (电话号码:   '1';行位置:“ 1706”)。

我得到了以下代码:

ParserContext context = new ParserContext();
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");    
context.XmlnsDictionary.Add("materialDesign", "http://materialdesigninxaml.net/winfx/xaml/themes");
context.XmlnsDictionary.Add("smtx", "clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML");
context.XmlnsDictionary.Add("l", "clr-namespace:UIControls;assembly=UIControls");
context.XmlnsDictionary.Add("d", "http://schemas.microsoft.com/expression/blend/2008");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
context.XmlnsDictionary.Add("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
context.XmlnsDictionary.Add("sys", "clr-namespace:System;assembly=mscorlib");
context.XmlnsDictionary.Add("xctk", "http://schemas.xceed.com/wpf/xaml/toolkit");


Encoding encoding = Encoding.UTF8;
var ecod = new System.IO.MemoryStream(encoding.GetBytes(xaml));
TabItem element = (TabItem)XamlReader.Load(ecod, context);
DataControl.Items.Add(element);

根据@ mm8的请求,这是一个较小的字符串xaml

string xaml = "<TabItem>" +
                  "<TabItem.Header>" +
                        "<StackPanel Orientation='Horizontal'>" +
                              "<TextBlock Text='Neutral' Foreground='Black' HorizontalAlignment='Center' VerticalAlignment='Center' Margin='0,0,0,0'/>" +
                        "</StackPanel>" +
                  "</TabItem.Header>" +
                  "<Grid Margin='0,20,0,0'>" +
                        "<Grid.ColumnDefinitions>" +
                             "<ColumnDefinition Width='1*'/>" +
                             "<ColumnDefinition Width='2*'/>" +
                             "<ColumnDefinition Width='1*'/>" +
                             "<ColumnDefinition Width='2*'/>" +
                        "</Grid.ColumnDefinitions>" +
                        "<StackPanel Grid.Column='0' Orientation='Vertical' HorizontalAlignment='Right'>" +
                             "<Label HorizontalAlignment='Right'>Sueldo:</Label>" +
                             "<Label HorizontalAlignment='Right'>Horas semanales:</Label>" +
                             "<Label HorizontalAlignment='Right'>Valor hora extra:</Label>" +
                             "<Label HorizontalAlignment='Right'>Valor hora extra nocturna:</Label>" +
                        "</StackPanel>" +
                        "<StackPanel Grid.Column='1' Orientation='Vertical'>" +
                             "<xctk:DoubleUpDown FontSize='16'/>" +
                             "<xctk:DoubleUpDown FontSize='16'/>" +
                             "<xctk:DoubleUpDown FontSize='16'/>" +
                             "<xctk:DoubleUpDown FontSize='16'/>" +
                        "</StackPanel>" +
                        "<StackPanel Grid.Column='2' Orientation='Vertical' HorizontalAlignment='Right'>" +
                             "<Label HorizontalAlignment='Right'>Valor de la hora(no extra):</Label>" +
                        "</StackPanel>" +
                        "<StackPanel Margin='10,0,0,0' Grid.Column='3' Orientation='Vertical' HorizontalAlignment='Left'>" +
                             "<TextBlock FontSize='18' Height='23'/>" +
                        "</StackPanel>" +
                 "</Grid>" +
             "</TabItem>";

那些控件给了我错误,尽管我在ParserContext中调用了该资源,但是却得到了好像没有在调用资源的错误。

如果我在XAML中手动添加它,就像一个超级按钮一样,其他dll资源正在运行,因此我认为BuildAction并不是全部。

我想念什么?还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

像这样在解析器中映射程序集:

context.XmlnsDictionary.Add("xctk", "clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit");

而不是这样:

context.XmlnsDictionary.Add("xctk", "http://schemas.xceed.com/wpf/xaml/toolkit");

解决了问题。