【发布时间】: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"