gpt4 book ai didi

c# - 在 XAML 堆栈布局中插入 cs stacklayout

转载 作者:行者123 更新时间:2023-11-30 21:35:43 26 4
gpt4 key购买 nike

我需要为我的应用程序插入一个 searchplace 栏,但我只在 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 命名

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

关于c# - 在 XAML 堆栈布局中插入 cs stacklayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48965987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com