【问题标题】:Internal Members of Friend Assembly Not Accessible in XAMLXAML 中无法访问 Friend 程序集的内部成员
【发布时间】:2019-12-04 00:59:18
【问题描述】:

问题陈述

我有两个程序集,一个主要包含数据协定,另一个包含 UI 层。

数据协定是公开的,并在 UI 程序集中的 XAML 中的 DataTemplate 标记中使用。

<DataTemplate DataType="{x:Type contracts:MyContract}"> 
///other stuff
</DataTemplate>

数据契约是这样引用的:

<xmlns:contracts="clr-namespace:Contracts.DataContracts,assembly=Contracts.DataContracts"

现在我想让所有的 DataContract 程序集都在内部,以保持较低的 API 表面。我在 DataContracts 程序集的 AssemblyInfo 类中添加了 UI 程序集作为友元程序集。但是,当我编译 UI 程序集时,我在 XAML 中收到以下错误:

Only public or internal classes can be used within markup. 'MyContract' type is not public or internal.

我检查了InternalVisibleTo 是否正常工作,因为我可以在后面的代码中访问其他内部内容。但在 XAML 中这是不可能的。

有哪些可能的方法来解决这个问题?

【问题讨论】:

    标签: c# wpf xaml access-modifiers


    【解决方案1】:

    不太清楚您尝试将哪些内容从程序集扩展到应用程序,但是当我在库中有 XAML 时,我倾向于在应用程序中以这种方式引用它。

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/PathInAssemblyIfNeeded/YourXAMLReferences.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    

    如果这没有帮助,请提供更多信息,说明您想要完成的工作。

    【讨论】:

    • 感谢您的回答。我不是指 XAML。我试图在我的 XAML 中引用在另一个程序集中标记为 internal 的类型。但我总是收到上述错误。我绝对可以访问我的 XAML 程序集中的内部类型(例如,在 .cs 文件中),但只有当我在 XAML 中引用它们时,我才会收到错误消息。所以我想知道这是否特定于 WPF?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2015-06-12
    • 2018-01-07
    • 2019-11-24
    • 2016-03-15
    • 2021-12-04
    相关资源
    最近更新 更多