How do i add a text entry,listview on map and icon in text entry?

Here is my code...

    ScrollView scroll = new ScrollView();
        Entry textbox1 = new Entry
        {
            Placeholder = "Search",
            TextColor = Color.Red,
        };
        Image img = new Image
        {
            Source = "icon.png"
        };
        var tapGestureRecognizer = new TapGestureRecognizer();
        tapGestureRecognizer.Tapped += (s, e) =>
        {
            ViewModel.AddressText = string.Empty;
        };
        img.GestureRecognizers.Add(tapGestureRecognizer);
        textbox1.SetBinding(Entry.TextProperty, new Binding("AddressText"));
        textbox1.TextChanged += OnTextChanged;
        ListView lstvw = new ListView()
        {
            HeightRequest = 0
        };
        lstvw.SetBinding(ListView.ItemsSourceProperty, new Binding("Addresses"));
        lstvw.ItemSelected += async (sender, selected) =>
        {
            await selectedlocation(sender, selected);
        };
        DataTemplate dt = new DataTemplate(typeof(TextCell));
        dt.SetBinding(TextCell.TextProperty, new Binding("Address"));
        lstvw.ItemTemplate = dt;
        var stack = new StackLayout { Spacing = 0};
         stack.Children.Add(img);
        stack.Children.Add(map);
        stack.Children.Add(textbox1);
        stack.Children.Add(lstvw);
        scroll.Content = stack;
        Content = scroll;

Thanks in advance...

Best Answer

  • RamprasadKRamprasadK US ✭✭
    edited October 2017 Accepted Answer

    Answer: I think we can't do it in Stack layout alone, so i used stack layouts in grid.

            Entry textbox1 = new Entry
            {
                Placeholder = "Search",
                TextColor = Color.Red,
            };
            Image img = new Image
            {
                Source = "icon.png"
            };
            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (s, e) =>
            {
                ViewModel.AddressText = string.Empty;
            };
            img.GestureRecognizers.Add(tapGestureRecognizer);
            textbox1.SetBinding(Entry.TextProperty, new Binding("AddressText"));
            textbox1.TextChanged += async (sender, eventArgs) =>
            {
                if (!string.IsNullOrWhiteSpace(ViewModel.AddressText))
                {
                    await ViewModel.GetPlacesPredictionsAsync();
                }
                else
                {
                    if (ViewModel.Addresses.Count > 0)
                    {
                        ViewModel.Addresses.Clear();
                    }
                }
            };
            ListView lstvw = new ListView()
            {
                HeightRequest = 200,
                BackgroundColor = Color.Transparent
            };
            lstvw.SetBinding(ListView.ItemsSourceProperty, new Binding("Addresses"));
            lstvw.ItemSelected += async (sender, selected) =>
            {
                await selectedlocation(sender, selected);
            };
            DataTemplate dt = new DataTemplate(typeof(TextCell));
            dt.SetBinding(TextCell.TextProperty, new Binding("Address"));
            dt.SetValue(TextCell.TextColorProperty, Color.Red);
            lstvw.ItemTemplate = dt;
    
            var scrolls = new ScrollView();
            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
    
            var stacklayout1 = new StackLayout();
            stacklayout1.SetValue(Grid.RowProperty, 0);
            stacklayout1.SetValue(Grid.ColumnProperty, 0);
            stacklayout1.Children.Add(map);
    
            var stacklayout2 = new StackLayout { BackgroundColor = Color.Transparent, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Start };
            stacklayout2.SetValue(Grid.RowProperty, 0);
            stacklayout2.SetValue(Grid.ColumnProperty, 0);
            stacklayout2.Children.Add(textbox1);
            // Grid.SetColumn(lstvw, 0);
            // Grid.SetRow(lstvw, 0);
            stacklayout2.Children.Add(lstvw);
    
            grid.Children.Add(stacklayout1);
            grid.Children.Add(stacklayout2);
            scrolls.Content = grid;
            Content = scrolls;
    

Answers

  • RamprasadKRamprasadK USMember ✭✭
    edited October 2017 Accepted Answer

    Answer: I think we can't do it in Stack layout alone, so i used stack layouts in grid.

            Entry textbox1 = new Entry
            {
                Placeholder = "Search",
                TextColor = Color.Red,
            };
            Image img = new Image
            {
                Source = "icon.png"
            };
            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (s, e) =>
            {
                ViewModel.AddressText = string.Empty;
            };
            img.GestureRecognizers.Add(tapGestureRecognizer);
            textbox1.SetBinding(Entry.TextProperty, new Binding("AddressText"));
            textbox1.TextChanged += async (sender, eventArgs) =>
            {
                if (!string.IsNullOrWhiteSpace(ViewModel.AddressText))
                {
                    await ViewModel.GetPlacesPredictionsAsync();
                }
                else
                {
                    if (ViewModel.Addresses.Count > 0)
                    {
                        ViewModel.Addresses.Clear();
                    }
                }
            };
            ListView lstvw = new ListView()
            {
                HeightRequest = 200,
                BackgroundColor = Color.Transparent
            };
            lstvw.SetBinding(ListView.ItemsSourceProperty, new Binding("Addresses"));
            lstvw.ItemSelected += async (sender, selected) =>
            {
                await selectedlocation(sender, selected);
            };
            DataTemplate dt = new DataTemplate(typeof(TextCell));
            dt.SetBinding(TextCell.TextProperty, new Binding("Address"));
            dt.SetValue(TextCell.TextColorProperty, Color.Red);
            lstvw.ItemTemplate = dt;
    
            var scrolls = new ScrollView();
            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
    
            var stacklayout1 = new StackLayout();
            stacklayout1.SetValue(Grid.RowProperty, 0);
            stacklayout1.SetValue(Grid.ColumnProperty, 0);
            stacklayout1.Children.Add(map);
    
            var stacklayout2 = new StackLayout { BackgroundColor = Color.Transparent, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Start };
            stacklayout2.SetValue(Grid.RowProperty, 0);
            stacklayout2.SetValue(Grid.ColumnProperty, 0);
            stacklayout2.Children.Add(textbox1);
            // Grid.SetColumn(lstvw, 0);
            // Grid.SetRow(lstvw, 0);
            stacklayout2.Children.Add(lstvw);
    
            grid.Children.Add(stacklayout1);
            grid.Children.Add(stacklayout2);
            scrolls.Content = grid;
            Content = scrolls;
    
Sign In or Register to comment.