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
).