Skip to content

Instantly share code, notes, and snippets.

@benbishop
Last active December 21, 2015 02:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benbishop/6232751 to your computer and use it in GitHub Desktop.
Save benbishop/6232751 to your computer and use it in GitHub Desktop.
A simple way to reuse your Strings.xml file from your Xamarin.Android app in Xamarin.IOS. To use, this all you have to do is include the Strings.xml file in the root of your project as a Bundle Resource.
public static class ResourceManager
{
static XDocument stringsDoc;
static XDocument integersDoc;
static ResourceManager ()
{
stringsDoc = XDocument.Load ("Strings.xml");
integersDoc = XDocument.Load ("Integers.xml");
}
public static string GetString(string key)
{
IEnumerable<XElement> strings =
(from el in stringsDoc.Root.Elements("string")
where (string) el.Attribute("name") == key
select el);
return strings.First ().Value;
}
public static string[] GetStringArray(string key)
{
IEnumerable<XElement> strings =
(from el in stringsDoc.Root.Elements("string-array")
where (string) el.Attribute("name") == key
select el);
var values = strings.First ().Descendants ().Select (x => x.Value).ToArray ();
for (int j = 0; j < values.Length; j++) {
values [j] = values[j].Replace("\\'", "'");
}
return values;
}
public static int GetInteger(string key)
{
IEnumerable<XElement> integers =
(from el in integersDoc.Root.Elements("integer")
where (string) el.Attribute("name") == key
select el);
return int.Parse(integers.First ().Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment