0

我有一个类,部分在 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并在这里仔细检查了部分类的要求。

我错过了什么??

4

3 回答 3

4

好的,我明白了:我从另一个项目复制并粘贴了 xaml 文件,并将文件粘贴到当前项目时,它的BuildAction属性更改为none,我没有注意到...将其切换到Page使 xaml 部分为人所知...

谢谢大家的帮助!

于 2013-01-10T15:32:37.773 回答
1

x:Class="TestElement.Views.ElementResource"

这是您的完整程序集名称吗?尝试添加所有部分,然后应该可以识别。

于 2013-01-10T14:41:10.767 回答
1

在您的 XAML 中,您是否缺少 ResourceDictionary 前面的“本地:”?

<local: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>

</local:ResourceDictionary>
于 2013-01-10T14:47:23.937 回答