【问题标题】:Cannot access propertyResources list from XAML无法从 XAML 访问 propertyResources 列表
【发布时间】:2019-04-11 00:33:22
【问题描述】:

我想从同一项目中的 XAML 文件访问我的 WPF 项目的“资源”属性。

  • 我添加了一个资源“Text123”并将访问修饰符设置为“公共”,
  • 我将 XAML 文件中的命名空间声明为 xmlns:p="clr-namespace:MyProject.Properties"
  • 我从按钮内容添加了指向属性的链接 Content="{x:Static p:Resources.Text123}"

因此,我从 Visual Studio 收到一条错误消息:名称“资源”不存在于命名空间“clr-namespace:MyProject.Properties”中。

我该怎么做才能摆脱这个错误?

Resources.resx 文件存在于项目中,并且似乎没问题。即使在Resources.Designer.vb 文件中,我也可以将我声明的资源视为Public ReadyOnly Property

P.S:生成的 Resources.Designed.vb 是:

Option Strict On
Option Explicit On

Imports System

Namespace My.Resources

    'This class was auto-generated by the StronglyTypedResourceBuilder
    'class via a tool like ResGen or Visual Studio.
    'To add or remove a member, edit your .ResX file then rerun ResGen
    'with the /str option, or rebuild your VS project.
    '''<summary>
    '''  A strongly-typed resource class, for looking up localized strings, etc.
    '''</summary>
    <Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0"),  _
     Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(),  _
     Global.Microsoft.VisualBasic.HideModuleNameAttribute()>  _
    Public Module Resources

        Private resourceMan As Global.System.Resources.ResourceManager

        Private resourceCulture As Global.System.Globalization.CultureInfo

        '''<summary>
        '''  Returns the cached ResourceManager instance used by this class.
        '''</summary>
        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)>  _
        Public ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
            Get
                If Object.ReferenceEquals(resourceMan, Nothing) Then
                    Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("MyProject.Resources", GetType(Resources).Assembly)
                    resourceMan = temp
                End If
                Return resourceMan
            End Get
        End Property

        '''<summary>
        '''  Overrides the current thread's CurrentUICulture property for all
        '''  resource lookups using this strongly typed resource class.
        '''</summary>
        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)>  _
        Public Property Culture() As Global.System.Globalization.CultureInfo
            Get
                Return resourceCulture
            End Get
            Set
                resourceCulture = value
            End Set
        End Property

        '''<summary>
        '''  Looks up a localized string similar to Cancel.
        '''</summary>
        Public ReadOnly Property Text123() As String
            Get
                Return ResourceManager.GetString("Text123", resourceCulture)
            End Get
        End Property

    End Module
End Namespace

【问题讨论】:

  • 您的项目是否构建并运行?
  • 错误中止构建过程
  • 你确定生成的类的名字是“Resource”而不是“Resources”吗?
  • 这是我的问题中的错字,抱歉。当然是“资源”。我更正了问题文本。错误仍然存​​在
  • 类的命名空间和名称是什么?

标签: wpf vb.net xaml properties


【解决方案1】:

将默认命名空间添加到命名空间声明中,即如果您的默认命名空间是“WpfApp1”并且Resources 模块位于My.Resources 命名空间中,则它应该是:

xmlns:p="clr-namespace:WpfApp1.My.Resources"

【讨论】:

  • 我按照你的建议做了。现在错误消息是“成员“Text123”无法识别或无法访问。
  • 何时构建?请发布生成的课程。
  • 我在原始帖子中添加了它。
  • 在我的文件夹中创建一个名为“Resource1.resx”的新 .resx 文件对我来说效果很好。您是否遇到任何其他构建错误?
  • 除了之前的错误,我收到警告“BC40056:在 Imports 'My.Resources' 中指定的命名空间或类型不包含任何公共成员或找不到。确保命名空间 ior类型已定义并包含至少一个公共成员。确保导入的元素名称不使用任何别名"
猜你喜欢
  • 2017-08-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多