错误:
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 的全部。
我缺少什么?还有其他方法可以做到这一点吗?