In Visual Studio 2022, I have created a Resources.resx file in the "Resources" folder located in the root of my C# WPF application project. In all classes, I can access the resources stored in that file using Resources.resourceName without an issue, but not in the MainWindow class. Whenever I try to access resources from the MainWindow.xaml.cs file only, I get the following error:
"CS1061. ’ResourceDictionary’ does not contain a definition for ’resourceName’ and no accessible extension method ’resourceName’ accepting a first argument of type ’ResourceDictionary’ could be found (are you missing a using directive or an assembly reference?)"
I am instantiating the MainWindow class in the Apps.xaml.cs file using the following code:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var mainWindow = new MainWindow();
}
Why am I getting the above error only within the MainWindow class and not in other classes?
As @Bilal suggested in the comment, the solution is to use fully qualified name to access the resource:.Resources.resourceName, e.g.
MyApp.Resources.resourceName.This is because Resources property is already defined in the FrameworkElement class which the Window class inherits from.