【发布时间】:2013-01-10 14:34:57
【问题描述】:
我有一个类,部分在 XAML 中定义,部分在代码中:
文件 ElementResource.xaml 如下所示:
<ResourceDictionary x:Class="TestElement.Views.ElementResource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestElement.Views"
xmlns:vm="clr-namespace:TestElement.ViewModels">
<DataTemplate x:Key="TestTemplate" DataType="{x:Type vm:TestElementViewModel1}">
</DataTemplate>
</ResourceDictionary>
类 *ElementResource" 的其余部分在文件 ElementResource.xaml.cs 中的代码中定义,如下所示:
using System.ComponentModel.Composition;
using System.Windows;
namespace TestElement.Views
{
[Export(typeof(ResourceDictionary))]
public partial class ElementResource : ResourceDictionary
{
}
}
由于某种原因,“代码隐藏”中无法识别 XAML 中定义的类部分:
另外,XAML 中定义的 DataTemplate 在初始化后不包含在资源字典中。
我尝试过构建和重建,Ctrl+Shift+s 并仔细检查了部分类here 的要求。
我错过了什么??
【问题讨论】:
-
ExportAttribute 服务器有什么用途?
-
它是使用 MEF(MS 可扩展性框架)进行依赖注入的导出属性。类的导出工作,只是缺少 XAML 部分......
-
知道了,所以这可能与您的问题无关。可能是一个愚蠢的问题,但我不得不问。您的 XAML 和代码隐藏在同一个项目中?
-
呵呵,是同一个文件夹,甚至..
-
DataTemplate 的一切都井井有条吗? Visual Studio 能够解析 TestElementViewModel1 以及该 XAML 文件中可能包含的任何其他内容?由于 XAML 中某处未报告的问题,可能 XAML 定义的部分或您的部分类未正确编译。
标签: wpf xaml code-behind partial-classes