How do I get rid of binding error using ResX/PCL with UWP?

Hi all,

I have a cross-platform (iOS, Android, UWP) solution that I have created a PCL called ApplicationResources that contains resource strings. I have it working fine but I get errors for any Xaml that references those strings because the strings are static properties created by PublicResXFileCodeGenerator. UWP does not seem to like to bind to static properties. The app compiles and runs fine but I would like to get ride of these errors.

I load reference to class that provides access to the strings in App.xaml merged dictionaries:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:applicationResources="using:ApplicationResources">
    <applicationResources:LocalizedStrings x:Key="Localization" />
</ResourceDictionary>

LocalizedStrings looks like this. AppStrings is the auto-generated class.

namespace ApplicationResources
{
    public class LocalizedStrings
    {
        public LocalizedStrings()
        {
            RefreshCulture();
        }

        public void RefreshCulture()
        {
            var languageCultureName = CultureInfo.CurrentUICulture.Name;
            Debug.WriteLine($"RefreshCulture: languageQualifier = {languageCultureName}");
            AppStrings.Culture = new CultureInfo(languageCultureName);
        }

        public AppStrings Strings { get; } = new AppStrings();
    }
}

The xaml refernce looks like this:

<TextBlock Grid.Row="1" Text="{Binding Strings.email, Source={StaticResource Localization}}" Margin="{StaticResource StandardTopMargin}"/>

The error is this:

ErrorXLS0518 Expected 'email' on type 'AppStrings' to be an instance member.

The error totally makes sense but don't know how to easily get rid of it.

I tried the approach mentioned at the bottom of this SO post but couldn't get it to build without errors:

https://stackoverflow.com/questions/47541558/expected-value-on-type-resource-to-be-an-instance-member

Seems if I had a generator that created properties that were not static I wouldn't get these errors.

I'm wondering if you folks have an idea of how to do this in a better way or whether I should just ignore these errors.

I believe the idea for this approach originally came from @Cheesebaron .

Ted

Best Answer

  • TedRogersTedRogers US ✭✭✭✭
    Accepted Answer

    x:Bind now lets you bind to static properties:

    <TextBlock Text="{x:Bind ar:AppStrings.app_copyright}"/>

Answers

Sign In or Register to comment.