1

In my solution I have the following project types:

Views

  • Windows Phone 8.1 (Universal)
  • Windows 8.1 (Universal)
  • Windows Phone 8.0

View Model

  • Portable project (namespace xxx.Common)

View Model

In the portable project I have a.resx file with Access Modifier set to Public (portable projects only support .resx files)

xxx.Commom\Resources\ViewResources.resx

Then to access the resource I have a wrapper called ResourceAccess.cs, I have to make a wrapper because .resx generates an internal constructor.

namespace xxx.Common.Resources
{
    public class ResourceAccessor
    {
        private static ViewResources _appResources;
        public static ViewResources AppResources
        {
            get
            {
                if(_appResources== null)
                {
                    _appResources= new ViewResources();
                }
                return _appResources;
            }
        }
    }
}

View

In the View for both Windows Phone 8.0 and Windows 8.1 projects I have a file called LocalizedStrings.cs

using xxx.Common.Resources;
namespace xxx
{
    public class LocalizedStrings
    {
        public ViewResources LocalizedResources { get { return ResourceAccessor.AppResources; } }
    }
}

App.xaml file

Windows Phone 8.0 project

<Application.Resources>
    <local:LocalizedStrings xmlns:local="clr-namespace:xxx" x:Key="LocalizedStrings"/>
</Application.Resources>

Windows 8.1 shared project

<Application
xmlns:local="using:xxx">
    <Application.Resources>
        <local:LocalizedStrings  x:Key="LocalizedStrings"/>
    </Application.Resources>
</Application>

Main.xaml

In the Windows Phone 8.0 project this works:

<TextBlock Text="{Binding Path=LocalizedResources.CommonResxText, Source={StaticResource LocalizedStrings}}"/>

But in the Windows 8.1 project the above does not work.

How do you reference a string in a .resx file in Windows 8.1 (WinRT)?

I have had a look at Using string resources from MSDN, but that does not help me, since I cannot change the resx into a resw in a portable project (that literally only references .Net).

Barnstokkr
  • 2,904
  • 1
  • 19
  • 34

2 Answers2

2

After a long stare at this question

I saw that my bindings in my Windows 8.1 project was wrong, and this is the correct binding

<TextBlock Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.CommonResxText}"/>

P.S.

This approach works... in Debug

When the project is in Release it doesn't.

In the auto-generated file ViewResources.Designer.cs

System.Resources.ResourceManager.GetString("CommonResxText", resourceCulture);

throws an exception, because the strings don't exist in the assembly; they're not compiled into the binary in Release

Read more about this issue here

Community
  • 1
  • 1
Barnstokkr
  • 2,904
  • 1
  • 19
  • 34
  • 1
    Huge +1 for the link to the msdn blog post. That saved my day, and by extension, the release of my current project! – Tomas Aschan Jun 08 '15 at 10:00
-2

Why do you need all the wrapper classes? Why not referring to the resource file contents directly?

To do so, you simply make the resource file generate code (with Access Modifier: Public in the combo box at the .resx screen) and then bind the text directly to the view, like this:

<TextBlock Text="{x:Static resx:ViewResources.MyText}" />

Just make sure to add resx as the xml namespace at your Window/UserControl:

<Window xmlns:resx="clr-namespace:xxx.Common.Resources" ... />
Rafael Costa
  • 248
  • 3
  • 13
  • 1
    Thanks for the answer, but it doesn't work, gives error: Static is not supported in Windows Phone/App project – Barnstokkr Nov 17 '14 at 13:32