locked
Using 'CustomResource' in DataTemplates or UserControls leads to an error: "No custom resource loader set."

    Question

  • In my Windows 8.1 application I'm using a the CustomResource markup extension to access localized strings from XAML. (http://msdn.microsoft.com/en-us/library/jj552942.aspx)

    As described I've derived a custom resource loader:

    public class TranslationSourceProvider : CustomXamlResourceLoader
        {
            readonly ResourceLoader _resourceLoader = new ResourceLoader();
    
            protected override object GetResource(string resourceId, string objectType, string propertyName, string propertyType)
            {
                return _resourceLoader.GetString(resourceId);
            }
        }

    And I'm setting it up in my App() like this: (additionally holding the instance in my App.cs to access it from application logic)

    CustomXamlResourceLoader.Current = new TranslationSourceProvider();
    ResourceLoader = new Windows.ApplicationModel.Resources.ResourceLoader();

    Now I can access my localizations from XAML like this:

    <TextBlock Text="{CustomResource AnyLocalizedString}" />

    The only problem occurs when accessing the localized string via CustomResource from any UserControl or DataTemplate not directly defined on any Page. An error will occur:

    "The text associated with this error code could not be found. No custom resource loader set."

    Despite this error the application will build and run and all localized strings will appear correctly, but the blue underlined Datatemplate and any large number of such errors in the Error List get really annoying.

    Any idea how to resolve this?

    I guess I just need to tell my custom UserControls and Resource files where the CustomResource loader is located?

    The error in a custom UserControl:
    Beside the error in the Error List the whole DateTemplate will get underlined blue.

    Monday, September 29, 2014 12:08 PM

Answers

  • Hi Sust,

    I can repro your issue and it seems that app cannot find the CoursePageAuthorReadMore resource at runtime. You need to debug your project to find out why TranslationSourceProvider class cannot find the string.

    There is an easy way to retire the string from resource file using static resource, can you tell me why you need to use CustomResource? Per my understanding, it’s no need, this approach makes your project more complex. Please see how to retire string from static resource file as the same result.

    1. Add a resource dictionary to your project.

    2. Merge the resource file into project in app.xaml.

    <Application
    
        x:Class="App15.App"
    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
        xmlns:local="using:App15">
    
        <Application.Resources>
    
            <ResourceDictionary>
    
                <ResourceDictionary.MergedDictionaries>
    
                    <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
    
                </ResourceDictionary.MergedDictionaries>
    
            </ResourceDictionary>
    
        </Application.Resources>
    
    </Application>
    

    3. Retire string in MainPage.xaml.

     

    <StackPanel>
    
                <GridView>
    
                    <GridView.ItemTemplate>
    
                        <DataTemplate>
    
                            <Button Content="{StaticResource CoursePageAuthorReadMore}"></Button>
    
                        </DataTemplate>
    
                    </GridView.ItemTemplate>
    
                </GridView>
    
            </StackPanel>
    

    If you still need to use CustomXamlResourceLoader, please post a test project. I will look into it. Use your OneDrive and share a link here.

    If I misunderstand you, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 30, 2014 9:44 AM
    Moderator

All replies

  • Hi Sust,

    I can repro your issue and it seems that app cannot find the CoursePageAuthorReadMore resource at runtime. You need to debug your project to find out why TranslationSourceProvider class cannot find the string.

    There is an easy way to retire the string from resource file using static resource, can you tell me why you need to use CustomResource? Per my understanding, it’s no need, this approach makes your project more complex. Please see how to retire string from static resource file as the same result.

    1. Add a resource dictionary to your project.

    2. Merge the resource file into project in app.xaml.

    <Application
    
        x:Class="App15.App"
    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
        xmlns:local="using:App15">
    
        <Application.Resources>
    
            <ResourceDictionary>
    
                <ResourceDictionary.MergedDictionaries>
    
                    <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
    
                </ResourceDictionary.MergedDictionaries>
    
            </ResourceDictionary>
    
        </Application.Resources>
    
    </Application>
    

    3. Retire string in MainPage.xaml.

     

    <StackPanel>
    
                <GridView>
    
                    <GridView.ItemTemplate>
    
                        <DataTemplate>
    
                            <Button Content="{StaticResource CoursePageAuthorReadMore}"></Button>
    
                        </DataTemplate>
    
                    </GridView.ItemTemplate>
    
                </GridView>
    
            </StackPanel>
    

    If you still need to use CustomXamlResourceLoader, please post a test project. I will look into it. Use your OneDrive and share a link here.

    If I misunderstand you, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 30, 2014 9:44 AM
    Moderator
  • I have same problem like the original poster. I think this is a bug in the development environment and it should be addressed. With your merged dictionary approach and using the strings as a static resource my question is: How would you localize application with this approach? Isn't placing the strings in the resource files done for the purpose to allow the application localization?
    Wednesday, November 12, 2014 4:46 PM