Spook
Spook

Reputation: 25927

XAML cannot reach localized resources

I'm having trouble with localized resources for sub-projects.

The main project (called Z) uses subproject (called ProjectsModule), which has its own resources. The subproject has two resource files:

Strings.resx
Strings.pl-PL.resx

The application is running in debug mode with Polish as current UI culture.

I'm getting the following exception:

System.Windows.Markup.XamlParseException: 'Operacja podawania wartości elementu 
„System.Windows.Markup.StaticExtension” wywołała wyjątek., numer wiersza 24, pozycja 52.'
ArgumentException: Obiektu StaticExtension 
„ProjectsModule.Resources.Strings.Projects_Config_Projects” nie można rozpoznać jako wyliczenia, pola 
statycznego ani właściwości statycznej.

Translated, it says:

"ProjectsModule.Resources.Strings.Projects_Config_Projects" cannot be recognized as enum, static field
or static property.

This happens, when window's XAML is loaded. So I did a test and modified window's constructor:

public ConfigurationWindow(ConfigurationWindowViewModel viewModel)
{
    // *** DEBUG ***
    System.Diagnostics.Debug.WriteLine(ProjectsModule.Resources.Strings.Projects_Config_Projects);
    // *** END DEBUG ***

    InitializeComponent();

    this.viewModel = viewModel;
    viewModel.Access = this;
    this.DataContext = viewModel;
}

And strangely I'm getting the following output:

Katalogi projektów
'Z.exe' (CLR v4.0.30319: Z.exe): Loaded     'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xaml.resources\v4.0_4.0.0.0_pl_b77a5c561934e089\System.Xaml.resources.dll'. Module was built without symbols.
Exception thrown: 'System.ArgumentException' in System.Xaml.dll
Exception thrown: 'System.Xaml.XamlObjectWriterException' in System.Xaml.dll
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Operacja podawania wartości elementu „System.Windows.Markup.StaticExtension” wywołała wyjątek., numer wiersza 24, pozycja 52.

The "Katalogi projektów" is exactly the resource I'm looking for - so it is clearly reachable. However, for some reason it is not reachable by the XAML parser (you can see the exception mentioned earlier just after properly resolved resource).

What am I doing wrong? Why the resource, which clearly is available (and, moreover, read from proper translated satellite assembly) is not available by the XAML parser?


Edit: In response to comments

Resource usage:

<Window ...
    xmlns:properties="clr-namespace:ProjectsModule.Resources"
</Window>

...

<TextBlock DockPanel.Dock="Top" Margin="3" Style="{StaticResource Header}" 
    Text="{x:Static properties:Strings.Projects_Config_Projects}"></TextBlock>

Culture is based on operating system culture. So it is set automatically.

Upvotes: 1

Views: 624

Answers (1)

thatguy
thatguy

Reputation: 22079

StaticExtension value cannot be resolved to an enumeration, static field, or static property.

Your .resx files must have a Public access modifier, otherwise you will get a XamlParseException at runtime, since the XAML parser cannot access the resources. It is Internal by default, so resources cannot be accessed from another assembly. To change the access modifier, go to the resource editor and change it in the header drop-down menu.

enter image description here

Alternatively, change the CustomTool in the resource file properties to PublicResXFileCodeGenerator.

enter image description here

Upvotes: 6

Related Questions