【问题标题】:Cannot create unknown type 'Canvas' when loading XAML control dynamically动态加载 XAML 控件时无法创建未知类型“画布”
【发布时间】:2019-02-21 17:00:38
【问题描述】:

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

我在执行 Xamlreader.Load 方法时遇到了问题,该方法引发了异常,指出父元素(“Canvas”)是未知类型。我做了一些研究,建议在 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);

来自 DB 的 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 以使用调用控件的本地程序集。我很感激我能得到的所有见解。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案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);
    

    【讨论】:

    • 天哪,我继续添加了“x”名称空间,但没有添加演示文稿。我很高兴这是一个简单的修复。现在一切正常。感谢您对这个愚蠢错误的见解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多