【问题标题】:VB WPF xaml file referencing My.ResourcesVB WPF xaml 文件引用 My.Resources
【发布时间】:2019-12-24 17:56:30
【问题描述】:

我无法让工作中的 C# WPF 项目的 VB 版本的 xaml 部分正常工作。

在 VB 项目 xaml 文件的顶部:

xmlns:resources="clr-namespace:My.Resources"

xaml 文件的第一条错误消息是:

"Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'My.Resources' that could not be found."

xaml 文件中对“资源”的典型引用:

<TextBlock FontSize="32" Text="{x:Static resources:Resources.Foo_ProjectName}" Style="{StaticResource HeaderTextBlock}" />

VB 项目没有“根命名空间”。如果我添加一个根命名空间 'Foo' 并将上述指令更改为 以下,没有区别 - 关于未定义的 CLR 命名空间的相同消息:

xmlns:resources="clr-namespace:Foo.My.Resources"

Resources.Designer.vb 文件概述(自动生成,因此我无法控制):

Namespace My.Resources
    ...
    Public Module Resources
        ...
        Public ReadOnly Property Foo_ProjectName() As String
            Get
                Return ResourceManager.GetString("Foo_ProjectName", resourceCulture)
            End Get
        End Property
        ...
    End Module
End Namespace

这个项目的工作 C# 版本:

xaml 文件:

xmlns:resources="clr-namespace:Foo.Properties"

Resources.Designer.cs 文件的部分代码

namespace Foo.Properties {
    public class Resources {
        ...

        public static string Foo_ProjectName {
            get {
                return ResourceManager.GetString("Foo_ProjectName", resourceCulture);
            }
        }
        ...
    }
}

【问题讨论】:

  • 我刚刚尝试戳这个,但我无法重现您看到的错误。我使用了xmlns:res="clr-namespace:My.Resourcesxmlns:res="clr-namespace:My,在命名空间声明中没有任何问题。但是,我没有任何成功然后提到那里的资源。我遇到了一个错误“成员无效,因为它没有合格的类型名称。”项目有一个默认的根命名空间。

标签: wpf vb.net xaml


【解决方案1】:

根据一些测试,我认为My.Resources 中的资源与来自 WPF xaml 的参考不兼容。似乎问题在于用于它的代码生成器将对资源的强类型引用放入Module(相当于C#中的static class)而不是Class(相当于class在 C# 中),而 xaml 编译器仅支持后者。这似乎是对My.Resources 使用特殊的“VbMyResourcesResXFileCodeGenerator”的功能,而对任何其他 .resx 文件使用“ResXFileCodeGenerator”。我怀疑这是 C# 项目中的默认资源与 VB 项目中的 My.Resources 相比的行为差异。

解决方法是不要使用My.Resources。而是在项目中使用自定义 .resx 文件。然后,您将能够按预期引用来自 xaml 的资源。

【讨论】:

  • 谢谢 - 在过去的几天里我一直是这样的,但你的回答让我相信是这样的。
猜你喜欢
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多