MonoTouch.Dialog.JsonElement.LoadString C# (CSharp) Method

LoadString() static private method

static private LoadString ( System.Json.JsonObject json, object data ) : Element
json System.Json.JsonObject
data object
return Element
		static Element LoadString (JsonObject json, object data)
		{
			string value = null;
			string caption = value;
			string background = null;
			NSAction ontap = null;
			NSAction onaccessorytap = null;
			int? lines;
			UITableViewCellAccessory? accessory;
			UILineBreakMode? linebreakmode;
			UITextAlignment? alignment;
			UIColor textcolor = null, detailcolor = null;
			UIFont font = null;
			UIFont detailfont = null;
			UITableViewCellStyle style = UITableViewCellStyle.Value1;

			foreach (var kv in json){
				string kvalue = (string) kv.Value;
				switch (kv.Key){
				case "caption":
					caption = kvalue;
					break;

				case "value":
					value = kvalue;
					break;

				case "background":
					background = kvalue;
					break;

				case "style":
					style = ToCellStyle (kvalue);
					break;

				case "ontap": case "onaccessorytap":
					string sontap = kvalue;
					int p = sontap.LastIndexOf ('.');
					if (p == -1)
						break;
					NSAction d = delegate {
						string cname = sontap.Substring (0, p);
						string mname = sontap.Substring (p+1);
						var mi = Type.GetType (cname).GetMethod (mname, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
						if (mi != null)
							mi.Invoke (null, new object [] { data });
					};
					if (kv.Key == "ontap")
						ontap = d;
					else
						onaccessorytap = d;
					break;

				case "lines":
					int res;
					if (int.TryParse (kvalue, out res))
						lines = res;
					break;

				case "accessory":
					accessory = ToAccessory (kvalue);
					break;

				case "textcolor":
					textcolor = ParseColor (kvalue);
					break;

				case "linebreak":
					linebreakmode = ToLinebreakMode (kvalue);
					break;

				case "font":
					font = ToFont (kvalue);
					break;

				case "subtitle":
					value = kvalue;
					style = UITableViewCellStyle.Subtitle;
					break;

				case "detailfont":
					detailfont = ToFont (kvalue);
					break;

				case "alignment":
					alignment = ToAlignment (kvalue);
					break;

				case "detailcolor":
					detailcolor = ParseColor (kvalue);
					break;

				case "type":
					break;

				default:
					Console.WriteLine ("Unknown attribute: '{0}'", kv.Key);
					break;
				}
			}
			if (caption == null)
				caption = "";
			if (font != null || style != UITableViewCellStyle.Value1 || detailfont != null || linebreakmode.HasValue || textcolor != null || accessory.HasValue || onaccessorytap != null || background != null || detailcolor != null){
				StyledStringElement styled;

				if (lines.HasValue){
					styled = new StyledMultilineElement (caption, value, style);
					styled.Lines = lines.Value;
				} else {
					styled = new StyledStringElement (caption, value, style);
				}
				if (ontap != null)
					styled.Tapped += ontap;
				if (onaccessorytap != null)
					styled.AccessoryTapped += onaccessorytap;
				if (font != null)
					styled.Font = font;
				if (detailfont != null)
					styled.SubtitleFont = detailfont;
				if (detailcolor != null)
					styled.DetailColor = detailcolor;
				if (textcolor != null)
					styled.TextColor = textcolor;
				if (accessory.HasValue)
					styled.Accessory = accessory.Value;
				if (linebreakmode.HasValue)
					styled.LineBreakMode = linebreakmode.Value;
				if (background != null){
					if (background.Length > 1 && background [0] == '#')
						styled.BackgroundColor = ParseColor (background);
					else
						styled.BackgroundUri = new Uri (background);
				}
				if (alignment.HasValue)
					styled.Alignment = alignment.Value;
				return styled;
			} else {
				StringElement se;
				if (lines == 0)
					se = new MultilineElement (caption, value);
				else
					se = new StringElement (caption, value);
				if (alignment.HasValue)
					se.Alignment = alignment.Value;
				if (ontap != null)
					se.Tapped += ontap;
				return se;
			}
		}