private static void BuildChildren(StackLayout view, SerializableStackLayout root)
 {
     foreach (SerializableViewElement child in root.Children)
     {
         view.Children.Add(BuildView(child));
     }
 }
示例#2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jo   = JObject.Load(reader);
            string  type = (string)jo["Type"];
            SerializableViewElement item;

            switch (type)
            {
            case "StackLayout":
                item = new SerializableStackLayout();
                break;

            case "Label":
                item = new SerializableLabel();
                break;

            default:
                item = new SerializableViewElement();
                break;
            }

            serializer.Populate(jo.CreateReader(), item);

            return(item);
        }
        private static StackLayout BuildStackLayout(SerializableStackLayout root)
        {
            StackLayout ToReturn = new StackLayout();

            ToReturn.Orientation       = GetStackOrientation(root.Orientation);
            ToReturn.HorizontalOptions = GetLayoutOptions(root.HorizontalOptions);
            ToReturn.VerticalOptions   = GetLayoutOptions(root.VerticalOptions);
            BuildChildren(ToReturn, root);
            return(ToReturn);
        }
        void BuildDynamicContent(string viewString)
        {
            viewString = @"{
	                            'Type': 'StackLayout',
	                            'Orientation': 'Vertical',
	                            'Children':[
		                            {
			                            'Type': 'StackLayout',
			                            'Orientation': 'Horizontal',
			                            'HorizontalOptions': 'CenterAndExpand',
			                            'VerticalOptions': 'CenterAndExpand',
			                            'Children':[
				                            {
					                            'Type': 'Label',
					                            'Text': 'Hello world!',
					                            'HorizontalOptions': 'CenterAndExpand'
				                            },
				                            {
					                            'Type': 'Label',
					                            'Text': 'Another label!',
                                                'TextColor': 'Blue',
					                            'HorizontalOptions': 'CenterAndExpand'
				                            }
			                            ]
		                            },
		                            {
			                            'Type': 'Label',
			                            'HorizontalOptions': 'CenterAndExpand',
			                            'VerticalOptions': 'CenterAndExpand',
			                            'Text': 'This label is not a child'
		                            }
	                            ]
                            }";

            SerializableStackLayout deserializedView = JsonConvert.DeserializeObject <SerializableStackLayout>(viewString, new SerializableViewElementConverter());
            StackLayout             content          = (StackLayout)ViewBuilder.BuildView(deserializedView);
            Button scanButton = new Button()
            {
                HorizontalOptions = LayoutOptions.CenterAndExpand, Text = "Start Scanning"
            };

            scanButton.Clicked += Scan_Clicked;

            content.Children.Add(scanButton);

            ScrollView scroll = new ScrollView();

            scroll.Content = content;

            Device.BeginInvokeOnMainThread(() =>
            {
                Content = scroll;
            });
        }