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

TedRogersTedRogers ✭✭✭✭USMember ✭✭✭✭

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

  • ColeXColeX Xamurai Member, Xamarin Team Xamurai
    edited August 2018

    Check https://forums.xamarin.com/discussion/67751/how-can-i-use-binding-with-static-property

    I think you can make AppStrings (not email inside it) be Static.

    Usage:

    Text="{Binding email, Source={x:Static local:LocalizedStrings.Strings}}"
    
  • TedRogersTedRogers ✭✭✭✭ USMember ✭✭✭✭

    @ColeX - x:Static does not exist in UWP.

  • TedRogersTedRogers ✭✭✭✭ USMember ✭✭✭✭
    Accepted Answer

    x:Bind now lets you bind to static properties:

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

  • kinekine ✭✭ FIMember ✭✭

    @TedRogers I have exactly same problem, but I cannot bind to static properties with x:Bind either. Can you share some details how to get it working? Does it require some minimum version of Windows 10 SDK? Are you using VS2017 or VS2019? I am using VS2027, minimum version is 16299, target version 17763.

  • TedRogersTedRogers ✭✭✭✭ USMember ✭✭✭✭

    @kine I am using Visual Studio 2017. My minimum version is 16299 and target is 17763 as well.

    My resx resources that I am referencing are in a PCL. I have this "PublicResXFileCodeGenerator" specified as the Custom Tool for the resx.

    In top of my xaml, I have this line:

    xmlns:ar="using:ApplicationResources"

    Here is how I used it for some Text in my xaml:

    Text="{x:Bind ar:AppStrings.add_folder}"

    Sample project attached.

    Ted

  • kinekine ✭✭ FIMember ✭✭

    @TedRogers Thanks for the details. In my project the error just disappeared at least for now. There were some unrelated errors in the same XAML and after I fixed those, also binding errors disappeared. So now it works without any code changes, I am using "Binding" still. I also upgraded to VS2019 and it still works. Quite strange...

Sign In or Register to comment.