Hello, I've been using the resx file manager in VisualStudio for my resource strings. It will make localizing my app a lot easier if I go that route down the road.
Here's my problem though. I have written a static class and function I use to get the strings in the C# code, but I cannot access the strings in my XAML code.
I am looking for a way to set my XAML <Label Text="" />
fields, and a way to set default values when setting up <Style>
tags.
I've tried this, but cant figure out how to make this work. This is the code I have been trying to get to work for my <Style>
tags, but it should be the same process for the regular XAML files too.
<ResourceDictionary
x:Class="MyAPp.Themes.AppStyles"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:resx="clr-namespace:BoomStick.Properties">
<Style x:Key="DataLabel" TargetType="Label">
<Setter Property="Text" Value="{x:Static resx:Resources.errSerial}" />
</Style>
</ResouceDictionary>
I get errors that Resources
cant be found, or I try variations that just end up with nothing being displayed.
Can someone help me figure it out?
I have read in some articles that by doing this you make the app less capable of localization.
I guess I could move all of my strings to a <ResourceDictionary>
of their own, I could then just localize the XAML file, and then use a switch/case
to choose between the various XAML file options.
Cheers!
Here are the accessor functions I created in my MyApp.Services
namespace, perhaps someone can benefit from them, or someone can recommend a better way to do it?
/// <summary>
/// Generate an object based collection for using in a control or array
/// <summary>
/// <param name="incoming">The name of the resource string.</param>
/// <returns></returns>
public static ObservableCollection<string> GetCSVArray(string incoming)
{
ObservableCollection<string> outputList = new ObservableCollection<string>();
if (incoming?.Length < 1 || String.IsNullOrEmpty(incoming) || string.IsNullOrEmpty(GetStringResource(incoming)))
{
outputList.Add("Error");
}
else
{
outputList = new ObservableCollection<string>(GetStringResource(incoming).Split(','));
}
return outputList;
}
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="name"> Name of string to retrieve. </param>
/// <returns> <see cref="string" /> from resources file. </returns>
public static object GetObjectResource(string name)
{
try
{
if (string.IsNullOrEmpty(name))
{
return "";
}
var temp = new System.Resources.ResourceManager(
"BoomStick.Properties.Resources",
typeof(App).GetTypeInfo().Assembly);
return temp.GetObject(name, null);
}
catch (System.Resources.MissingManifestResourceException mmre)
{
DebugTools.LogException(mmre);
}
catch (Exception ex)
{
DebugTools.LogException(ex);
}
return null;
}