【发布时间】:2012-09-19 08:21:05
【问题描述】:
我正在开始一个新项目,并将我的项目结构定位在 this question 推荐的结构上。
现在我看到了奇怪的行为。当我在 View-XAML 中设置数据上下文时,在运行时找不到它(获取XamlParseException)。当我在代码隐藏文件的构造函数中设置它时,一切正常。
这是使用不同程序集时的官方(记录)行为,还是我做错了什么?
代码:
不工作:
MainView.xaml:
<UserControl x:Class="ViewsRoot.Views.MainView"
xmlns:baseControls="clr-namespace:BaseControls;assembly=BaseControls"
xmlns:viewModels="clr-namespace:ViewModelsRoot;assembly=ViewModelsRoot">
<UserControl.DataContext>
<viewModels:ShellViewModel />
</UserControl.DataContext>
MainView.xaml.cs
public MainView()
{
InitializeComponent();
// No DataContext set in codebehind-file
}
工作:
MainView.xaml:
<UserControl x:Class="ViewsRoot.Views.MainView"
xmlns:baseControls="clr-namespace:BaseControls;assembly=BaseControls"
xmlns:viewModels="clr-namespace:ViewModelsRoot;assembly=ViewModelsRoot">
<!--<UserControl.DataContext>
<viewModels:ShellViewModel />
</UserControl.DataContext> -->
MainView.xaml.cs:
public MainView()
{
InitializeComponent();
DataContext = new ViewModelsRoot.ShellViewModel();
}
更新:
异常文本是:
{"文件或程序集\" ViewModelsRoot, PublicKeyToken = null \"或其依赖项之一未找到。系统找不到指定的文件。"}
我能看到的唯一内部异常是System.IO.FileNotFoundException。
更新 2:
感谢 cmets,但我没有忘记命名空间。我在这里缩短了它以显示代码,但我(再次)进行了双重和三重检查。 DataContexts 命名空间也由智能感知填充。整个<viewModels:ShellViewModel /> 由intelli-sense 编写。所以它是在设计时发现的......所以还有什么想法吗?
更新 3: xaml 被“正确”解析,因为我能够将 DataContext 绑定到同一程序集中的类。
【问题讨论】:
-
内部有异常吗?
-
@Guillaume 请查看我更新的问题:)
-
您可能忘记了
UserControl中的一些命名空间。检查这是否有帮助:stackoverflow.com/questions/8852912/xamlparseexception-in-view -
@Guillaume 谢谢,我再次更新了我的问题。 :)
-
嗯,我知道我过去遇到过这个异常。检查您的视图模型程序集中使用的所有引用是否也在您的视图之一中引用。正如内部异常所述,某些文件丢失了。由你来找到哪一个!