Last active
December 21, 2015 02:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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