11

I've started creating a Windows Store App for Windows 8.1 and now I encountered a problem concerning localization.

I would like to display a string resource from a .resw file at design time, but every attempt to do so has failed, even though it works at runtime.

When using the x:Uid attribute, I still have to supply the Text property (i.e. for a TextBlock) and I don't like to write the text twice.

I also tried creating a property for the string on the viewmodel:

public string Title
{
    get { return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title"); }
}

This is working at runtime, but at design time it is blank.

So the question is, is there a way to display resources from a .resw file in the XAML-designer?

More specifically, does the ResourceManager class allow .resw files to be read at design time?

Thanks for your help, Lucas

1
  • 1
    Added a new fix which should give you the designer functionality you desire(d). Jun 3, 2014 at 0:28

2 Answers 2

11

Old Method

So, there are a couple of things you can do.

The first (and simplest, given that you're using x:Uid already) is to just supply the text into the Text field. The x:Uid-related value will overwrite whatever is in there.

<TextBlock Text="MyText" x:Uid="MainView_Title"/>

The second method is to use the property like you already have, and then check to see if the app is in Design Time (through a couple of different methods), then return a constant value if it is and the Resource if it is not.

public string Title
{
     if(ViewModelBase.IsInDesignTimeStatic) //Mvvm Light's easy accessor
         return "My Text";
     return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title");
}

Hope this helps and happy coding!

Edit: There appears to be a new way to do this, at least as of Windows 8.1.

New Method

  • Create a class which references a ResourceLoader (similar to the property described above).
  • Create an indexed property accessor which accepts a string key and return the value from the ResourceLoader.

    public class LocalizedStrings
    {
        public string this[string key]
        {
            get
            {
                return App.ResourceLoader.GetForViewIndependentUse().GetString(key);
            }
        }
    }
    
  • In your App.xaml, define a StaticResource of this type.

    <Application.Resources>
        <ResourceDictionary>
            <common:LocalizedStrings x:Key="Localized"/>
        </ResourceDictionary>
    </Application.Resources>
    

Now, when you want to access your property with entry key MainView_Title, use this. It's more verbose, but it should translate both in the designer and in the app itself.

<TextBlock Text="{Binding Source={StaticResource Localized}, Path=[MainView_Title]}" />

You can shuffle it around to be a bit more readable if you'd like, such as:

<TextBlock Text="{Binding [MainView_Title], Source={StaticResource Localized}}" />
3
  • Thanks for the reply. I'm afraid this is exactly what I'm trying to avoid. I don't like to write something twice, because if I'm going to change a text in the resource I would then also have to update this text everywhere it is used in the XAML and in my opinion that is an unnecessary redundancy.
    – loreggia
    Nov 16, 2013 at 1:30
  • Unfortunately, from my research this is what will be required. The design surface is really for sample data as opposed to the actual data. Nov 18, 2013 at 20:40
  • Well then I'll have to try to manage without this. Thanks nonetheless.
    – loreggia
    Nov 19, 2013 at 22:30
0

This is an old thread, but since Nate provided such an elegant solution to the problem for Win8.1 I figured I'd ask here...

After much investigation and experimentation, Nate's solution does not appear to work for UWP apps for Win10 under VS2017 Community. The LocalizedString approach works just fine at runtime, but it appears

App.ResourceLoader.GetForViewIndependentUse().GetString(key);

refuses to return anything except String.Empty during design time. I've done a lot of experimenting and things like

ResourceContext.GetForViewIndependentUse().QualifierValues

Seem to be identical between runtime (working) and design time (not working).

I was wondering if anyone has encountered this and solved it. Nate? :)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.