【问题标题】:XamlObjectWriter and namespaces via configuration (cannot use XmlnsDefinitionAttribute)XamlObjectWriter 和命名空间通过配置(不能使用 XmlnsDefinitionAttribute)
【发布时间】:2014-12-03 05:28:06
【问题描述】:

我正在使用 Systeml.Xaml 创建一个基于 XAML 的自定义布局引擎。由于它是跨平台的,95% 的类型都定义在 Portable Class Libraries 中,因此不可能使用XmlnsDefinitionAttribute 来“装饰”默认程序集。

我想使用 XamlReader 来读取我的 XAML 格式文件。问题是我的对象属于几个命名空间。目前,如果我希望 XamlReader 能够实例化每个对象,我必须指定每个对象来自哪个命名空间,如下所示:

<Window Title="Title" xmlns="clr-namespace:Perspex.Win32;assembly=Perspex.Win32"
    xmlns:r="clr-namespace:Perspex;assembly=Perspex.Base">
    <Window.Content>
        <TextBlock xmlns="clr-namespace:Perspex.Controls;assembly=Perspex.Controls" Text="{r:Binding}" />    
    </Window.Content>    
</Window>

如何使 XamlReader 具有一组隐含的默认命名空间,以便在不指定命名空间的情况下发现它们?

编辑:我目前的代码是这样的。我正在使用 XamlXmlReader 来执行此操作。

    private static object Load(XmlReader reader)
    {          
        XamlXmlReader xamlXmlReader = new XamlXmlReader(reader, xamlSchemaContext);
        XamlObjectWriter writer = new XamlObjectWriter(
            xamlSchemaContext,
            new XamlObjectWriterSettings
            {
                XamlSetValueHandler = SetValue,                                        
            });

        while (xamlXmlReader.Read())
        {
            writer.WriteNode(xamlXmlReader);
        }

        object result = writer.Result;

        return result;
    }

如您所见,我尝试使用带有一些默认程序集的 XamlSchemaContext,但它不起作用:(

【问题讨论】:

  • 如何使用带有 ParserContext 参数的 XamlReader.Load 重载,该参数具有 XmlnsDictionary 属性?
  • 对不起,我忘了说:我正在使用 XamlXmlReader。查看已编辑的问题。我将发布我拥有的当前代码。感谢您的回答!
  • @Clemens 过去似乎是这样工作的。 XmlnsDictionary 是 ParserContext 的一部分,不再适用于 .NET 4.0 和更高版本。像 XamlObjectWriter 这样的对象会收到一个 XamlSchemaContext :( 不幸的是,thd 文档并没有提供太多关于它的信息。

标签: c# wpf xaml portable-class-library


【解决方案1】:

您是否考虑过使用程序集级别的 XmlnsDefinitionAttribute?它允许您定义一个 xmlns 映射到一个或多个程序集中的多个 CLR 命名空间。 谢谢,罗伯

【讨论】:

  • 不幸的是,我们需要“膨胀”(读取)的元素是在可移植类库中定义的。我们不能使用该属性,因为它不是 PCL 的 .NET 子集的一部分:(
猜你喜欢
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
相关资源
最近更新 更多