【问题标题】:Partial class in MyClass.xaml not recognized by partial class in MyClass.xaml.csMyClass.xaml 中的部分类无法被 MyClass.xaml.cs 中的部分类识别
【发布时间】: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


【解决方案1】:

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

谢谢大家的帮助!

【讨论】:

    【解决方案2】:

    x:Class="TestElement.Views.ElementResource"

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

    【讨论】:

    • 我知道,它不应该是,但是是的,它是完整的程序集名称。
    【解决方案3】:

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

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

    【讨论】:

    • ResourceDictionaryElementResource (System.Windows.ResourceDictionary) 的基类...所以,不幸的是,不起作用...
    • 哦,好的。不确定这是否是您在 TestElement.Views 命名空间中编写的另一个类。
    猜你喜欢
    • 2021-06-30
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2016-12-17
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    相关资源
    最近更新 更多