WPF Markup.XamlReader.Load 无法设置 DependencyProperty

本文关键字:设置 DependencyProperty Load Markup XamlReader WPF | 更新日期: 2023-09-27 18:36:05

我无法使用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;  

我在程序集中有以下依赖项属性;

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 文件中的"unkown 成员"设置为如下所示的 ModelVisual3D 对象:

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

WPF Markup.XamlReader.Load 无法设置 DependencyProperty

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

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