【问题标题】:WPF Markup.XamlReader.Load cannot set DependencyPropertyWPF Markup.XamlReader.Load 无法设置 DependencyProperty
【发布时间】:2013-10-18 10:16:02
【问题描述】:

我无法使用 Markup.XamlReader.Load 方法打开具有自定义命名空间的 .xaml 文件。我喜欢这样:

stream = openFileDialog1.OpenFile();
System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext();
parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
parserContext.XmlnsDictionary.Add("ex", "clr-namespace=Extensions;assembly=Extensions");

viewport = System.Windows.Markup.XamlReader.Load(stream, parserContext) as Viewport3D;  

我在程序集中有以下 DependencyProperty;

namespace Extensions
{
    public class Ext
    {
        public static DependencyProperty NameProperty = DependencyProperty.RegisterAttached("Name", typeof(string), typeof(Ext));

        public static string GetName(DependencyObject target)
        {
            return (string)target.GetValue(NameProperty);
        }

        public static void SetName(DependencyObject target, string name)
        {
            target.SetValue(NameProperty, name);
        }
    }
}

我的问题是我在 XamlReader.Load 方法中收到一个 XamlParseException,告诉我:无法设置未知成员 {clr-namespace=Extensions;assembly=Extensions}Name。

.xaml 文件中的“未知成员”设置为 ModelVisual3D 对象,如下所示:

我能找到的关于这个错误的所有信息都建议我做我已经尝试过的事情。请帮帮我!

【问题讨论】:

  • 您确定调用XamlReader.Load 的类可以访问Extensions 程序集吗?它是否包含正确的参考和using 声明?
  • 是的,只是为了确保我做了简单的测试:
  • 文本框测试 = new TextBox(); Extensions.Ext.SetName(test, "Test"); test.Text = Extensions.Ext.GetName(test); //它将文本设置为“测试”
  • .xaml 文件中的“未知成员”脱离了原来的问题:ex:Name="MyName"

标签: c# .net wpf xaml


【解决方案1】:

您需要在 XAML 中为附加属性指定类名和属性名。例如

<TextBox ex:Ext.Name="MyName"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多