动态加载XAML控件时无法创建未知类型“ Canvas”

时间:2019-02-21 00:52:29

标签: c# wpf xaml

我正在尝试构建WPF用户控件,以动态加载Canvas元素。存储在数据库中的是Xaml画布图形的模板,我想在加载到Viewbox时调用它。

在执行Xamlreader.Load方法时遇到麻烦,该方法抛出异常,说明父元素(“画布”)是未知类型。我已经进行了一些研究,建议您在xamlXmlreaderSettings对象中定义LocalAssembly并将其传递给阅读器。

我的代码如下:

UserControl1.xaml.cs:

StringReader stringReader = new StringReader(cart.svgtemplate); //this a string retrieved from DB that has the XAML
        XmlReaderSettings settings = new XmlReaderSettings { NameTable = new NameTable() };            
        XmlNamespaceManager xmlns = new XmlNamespaceManager(settings.NameTable);
        xmlns.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        XmlParserContext context = new XmlParserContext(null, xmlns, "", XmlSpace.Default);
        XmlReader xmlReader = XmlReader.Create(stringReader,settings, context);
        var xamlXmlReaderSettings = new XamlXmlReaderSettings()
        {
            LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly()
        };
        XamlXmlReader xamlXmlReader = new XamlXmlReader(xmlReader, xamlXmlReaderSettings);

        this.viewbox.Child = (UIElement)System.Windows.Markup.XamlReader.Load(xamlXmlReader);

数据库中的XAML是

<Canvas x:Name="canvas" Width="100" Height="90" RenderTransformOrigin="0.5,0.5" Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}">
        <Rectangle Canvas.Left="1" Canvas.Top="1" Width="95" Height="80" Name="rect442" Fill="#ffff00"/>
        <Rectangle Canvas.Left="1" Canvas.Top="1" Width="1.75" Height="80" Name="rect446" Fill="#0000ff"/>
        <Rectangle  Canvas.Left="2.75" Canvas.Top="79.25" Width="92.5" Height="1.75" Name="rect450" Fill="#0000ff"/>
        <Rectangle  Canvas.Left="94.25" Canvas.Top="1" Width="1.75" Height="80" Name="rect454" Fill="#0000ff"/>
        <Rectangle  Canvas.Left="2.75" Canvas.Top="42.5" Width="72" Height="1.75" Name="rect458" Fill="#0000ff"/>
        <Rectangle  Canvas.Left="74.75" Canvas.Top="1" Width="1.75" Height="78.25" Name="rect462" Fill="#0000ff"/>
....

UserControl1.xaml:

<UserControl x:Name="userControl" x:Class="Smart_Cart_Sample.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Smart_Cart_Sample"
         mc:Ignorable="d" 
         d:DesignHeight="700" d:DesignWidth="800">
<Viewbox x:Name="viewbox" Stretch="Fill" Width="{Binding Width, ElementName=userControl}" Height="{Binding Height, ElementName=userControl}" RenderTransformOrigin="0.5,0.5">
    </Viewbox>
    </UserControl>

我不确定执行程序集的引用方式是否正确。我希望读入XAML以使用调用控件的本地程序集。我很感谢我对此的所有见识。

1 个答案:

答案 0 :(得分:1)

缺少默认名称空间:

var settings = new XmlReaderSettings { NameTable = new NameTable() };
var xmlns = new XmlNamespaceManager(settings.NameTable);

// here
xmlns.AddNamespace("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
xmlns.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");

var context = new XmlParserContext(null, xmlns, "", XmlSpace.Default);
var xmlReader = XmlReader.Create(stringReader, settings, context);

viewbox.Child = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);