将资源字符串与 wpf 中的视图绑定时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19586401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:56:54  来源:igfitidea点击:

error in binding resource string with a view in wpf

.netwpf

提问by Hassan Akhtar

I am trying to bind a static string defined in the Resources.resx called title, When i run the application i get the following error below. Upon investigating the error in this forum i found that generated Resources class is marked internal and WPF is trying to resolve resource from another assembly so it wont succeed until your resources are marked as public. Same I have a single project which contains my views and resources defined i dont need any friend assembly, i believe internal class can be accessed within the same assembly. I found a similar post by a fellow Click herein which he suggested to use ResXFileCodeGenerator. Please have your say on this

我正在尝试绑定在 Resources.resx 中定义的名为 title 的静态字符串,当我运行应用程序时,出现以下错误。在调查此论坛中的错误后,我发现生成的资源类被标记为内部,WPF 正在尝试从另一个程序集中解析资源,因此在您的资源被标记为公共之前它不会成功。同样,我有一个包含我定义的视图和资源的项目,我不需要任何朋友程序集,我相信可以在同一个程序集中访问内部类。我发现了一个类似的帖子点击这里他建议使用 ResXFileCodeGenerator。请就此发表您的看法

System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
Message='Provide value on 'System.Windows.Markup.StaticExtension' threw an exception.' Line number '11' and line position '9'.
Source=PresentationFramework
LineNumber=11
LinePosition=9
StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at TestApp.MainWindow.InitializeComponent() in ~\TestApp\TestApp\MainWindow.xaml:line 1
   at TestApp.MainWindow..ctor() in ~\TestApp\TestApp\MainWindow.xaml.cs:line 28
InnerException: System.ArgumentException
   HResult=-2147024809
   Message='TestApp.Properties.Resources.Firstname' StaticExtension value cannot be resolved to an enumeration, static field, or static property.
   Source=System.Xaml
   StackTrace:
        at System.Windows.Markup.StaticExtension.ProvideValue(IServiceProvider serviceProvider)
        at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
   InnerException: 

回答by Sheridan

It seems that you need to use the PublicResXFileCodeGeneratorgenerator. Have you tried right clicking on the resource file in the Visual Studio Solution Explorer and selecting the Propertiesoption and then setting the Custom Toolproperty to PublicResXFileCodeGenerator? You can also set the Access Modifierproperty to Publicin the resources designer window in Visual Studio.

看来你需要使用PublicResXFileCodeGenerator生成器。您是否尝试过在 Visual Studio 解决方案资源管理器中右键单击资源文件并选择该Properties选项,然后将Custom Tool属性设置为PublicResXFileCodeGenerator?您还可以在 Visual Studio 的资源设计器窗口中将该Access Modifier属性设置为Public