【问题标题】:Insert a cs stacklayout in a XAML stack layout在 XAML 堆栈布局中插入 cs stacklayout
【发布时间】:2018-02-24 18:04:23
【问题描述】:

我的应用程序需要插入一个searchplace 栏,但我只在 C# 中看到了一个 sn-p,我在该页面中的应用程序已经有一个堆栈布局,我不能插入所有这两个,因为如果我只运行应用程序,只渲染 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 中已存在的内容。如果你想插入 XAML,你需要给你的 XAML stacklayout 一个 Name 属性,以及 Add children to it

标签: c# xaml xamarin mobile


【解决方案1】:

首先,在您的 XAML 中为您现有的 StackLayout 命名

<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)

【讨论】:

  • 谢谢,但我不明白为什么它不呈现此搜索栏,现在页面工作正常,但没有我要添加的搜索栏。感谢您的宝贵时间
  • 尝试给你的 SearchBar 一个明确的 HeightRequest。而且不清楚为什么不将所有 UI 定义都放在 XAML 中
  • 抱歉,我知道问题是 android 7 不使用 xamarin 呈现搜索栏...,我在 6.0 上试了一下,它工作正常,谢谢您的时间
猜你喜欢
  • 1970-01-01
  • 2011-12-24
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2016-06-23
  • 1970-01-01
相关资源
最近更新 更多