在XAML堆栈布局中插入CS堆栈



我需要插入 searchplace bar的应用,但是我只在C#中看到一个摘要,该页面中的应用程序已经具有堆栈布局,我无法插入所有这两个是因为如果我仅运行应用程序,则c#是渲染的,有人可以帮助我?

xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         xmlns:control="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
          xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
         x:Class="Sport.NewItemPage"
         Title="New Item">
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Save" Clicked="Save_Clicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
   <<ScrollView>
        <StackLayout Spacing="15" Padding="15">
            <StackLayout Orientation="Horizontal" >
                <Label Text="Title" FontSize="Medium" />
                <Label x:Name="TipoPost" Text="Private" HorizontalOptions="EndAndExpand" />
                <Switch x:Name="Swi" Toggled="Switch_toggled" HorizontalOptions="End" />
            </StackLayout>
            <Entry Placeholder="Title" Text="{Binding Title}" FontSize="Small" />
            <Button x:Name="DataButton"
                    Text="Select a date" 
                    FontSize="Medium"
                    BorderColor="White"
                    BackgroundColor="White"  
                    Clicked="Choose_date" />
            <Label x:Name="DataCamp" HorizontalOptions="CenterAndExpand" FontSize="Medium"/>
            <StackLayout Orientation="Horizontal">
                <Image Source="clock.jpg" HorizontalOptions="Start" VerticalOptions="CenterAndExpand" HeightRequest="25"/>
                <Label x:Name="orario" Text="Choose a time" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/>
                <TimePicker x:Name="Picker" HorizontalOptions="End" VerticalOptions="CenterAndExpand" PropertyChanged="Picker_PropertyChanged" />
            </StackLayout>
            <StackLayout Orientation="Horizontal">
                <SearchBar Placeholder="Search" SearchCommand="{Binding SearchCommand}" 
                           SearchCommandParameter="{Binding Path=Text,
                           RelativeSource={RelativeSource Self}}"/>
                <Image Source="position.png" HorizontalOptions="Start" VerticalOptions="Center" HeightRequest="25"/>
                <Entry x:Name="loc" Placeholder="Where is your event?" Text="{Binding Place}" FontSize="Medium" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"/>

            </StackLayout>
            <Label Text="Description" FontSize="Medium" />
            <Entry x:Name="des" Placeholder="Enter a description" Text="{Binding Description}" FontSize="Medium" Margin="0"  />
            <StackLayout Orientation="Horizontal">
                <Label Text="How many people can join this event?" FontSize="Medium" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/>
                <Button x:Name="MorePeople" Text="-" HeightRequest="25" WidthRequest="30" HorizontalOptions="EndAndExpand"/>
                <Entry Keyboard="Numeric" Text="{Binding Participants}" Placeholder="People" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" />
                <Button x:Name="MinusPeople" Text="+" HeightRequest="25" WidthRequest="30" HorizontalOptions="EndAndExpand"/>
            </StackLayout>
        </StackLayout>
    </ScrollView>
</ContentPage.Content>

,C#是

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.Collections.ObjectModel;
using Xamarin.Forms.Maps;
using Firebase.Xamarin.Database;
using Firebase.Xamarin.Database.Query;
using System.ComponentModel;
using System.Threading.Tasks;
using Newtonsoft.Json ;
using System.Net.Http;
using Sport.Services;
//Xam.Plugin.Geolocator
namespace Sport
{
    public partial class NewItemPage : ContentPage
    {
        private const string API_KEY = "String";
        public Item Item { get; set; }
        public NewItemPage()
        {
            InitializeComponent();
            BindingContext = Item = new Item();
            var stack = new StackLayout();
            var search = new SearchBar();
           var listView = new ListView();
            stack = new StackLayout();
            search = new SearchBar();
            listView = new ListView();
            stack.Children.Add(search);
            stack.Children.Add(listView);   

            search.TextChanged += async (sender, e) =>
            {
                try
                {
                    var searchBar = (SearchBar)sender;
                    var client = new HttpClient();
                    var url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + searchBar.Text + "&types=geocode&key=" + API_KEY;
                    var json = await client.GetStringAsync(url);
                    var predictionList = JsonConvert.DeserializeObject<GooglePlaceResult>(json);
                    var srcList = new List<string>();
                    foreach (var place in predictionList.predictions)
                    {
                        srcList.Add(place.description);
                    }
                    listView.ItemsSource = srcList;
                }
                catch (System.Exception)
                {
                    await DisplayAlert("error", "unknown error", "ok");
                }
            };
            Content = stack;
        }
        //metodi
        async void Save_Clicked(object sender, EventArgs e)
        {
            if (Item.Title == null || Item.Title.Equals("") || Item.Date == null)
            {
                await DisplayAlert("Error", "You must insert a title and a date for your event", "Ok");
            }
            else
            {  
                await DatabaseManager.DefaultManager.SaveTaskAsync(Item);
                await DisplayAlert("Item saved", "You saved the item " + Item.Title, "Ok");
                await Navigation.PopToRootAsync();
            }
        }
        async void Choose_date(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new Pagine.CalendarPage(Item, DataCamp));
        }
        public void Switch_toggled()
        {
            if (Swi.IsToggled)
            {
                TipoPost.Text = "Public";
                Item.IsPublic = true;
            }
            else
            {
                TipoPost.Text = "Private";
                Item.IsPublic = false;
            }
        }
        private void Picker_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("Time"))
                Item.Time = Picker.Time.ToString();
        }
    }
}

首先,在您的XAML中给您现有的stacklayout a name

<ContentPage.Content>
  <<ScrollView>
    <StackLayout x:Name="OuterStack" Spacing="15" Padding="15">

然后在您的C#中将您的新布局添加到现有布局

// DON'T do this - it will replace the content in your XAML
Content = stack;
// instead, do this - add to the layout already defined in the XAML
OuterStack.Children.Add(stack)

相关内容

  • 没有找到相关文章

最新更新