Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in binding resource string with a view in WPF

Tags:

.net

wpf

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 on this site, I found that the generated Resources class is marked internal and WPF is trying to resolve the resource from another assembly, so it won't succeed until your resources are marked as public. The same, I have a single project which contains my views and resources defined.

I don’t need any friend assembly. I believe the internal class can be accessed within the same assembly. I found a similar post by a fellow user in which he suggested to use ResXFileCodeGenerator. What is your say on this?

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:
like image 932
Hassan Akhtar Avatar asked Oct 25 '13 09:10

Hassan Akhtar


1 Answers

It seems that you need to use the PublicResXFileCodeGenerator generator.

Try right clicking on the resource file in the Visual Studio Solution Explorer and selecting the Properties option and then setting the Custom Tool property to PublicResXFileCodeGenerator. You can also set the Access Modifier property to Public in the resources designer window in Visual Studio.

like image 148
Sheridan Avatar answered Oct 20 '22 21:10

Sheridan