.net 搜索栏未显示

gev0vcfq  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(119)

在我的页面中,在Windows机器上,搜索栏显示正确,但在移动的iOS上,当我测试时,它没有显示

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:root="clr-namespace:Orbis.Mobile"
             x:Class="Orbis.Mobile.ActivitiePage"
             xmlns:viewmodel="clr-namespace:Orbis.Mobile.ViewModels"
             Title="">

    <ContentPage.Resources>
        <root:BytesToImageConverter x:Key="ToImage" />
    </ContentPage.Resources>
    <ContentPage.Content>
        <ScrollView>
            <StackLayout Orientation="Vertical">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <BoxView x:Name="Box" BackgroundColor="White" />
                    <!--<SearchBar 
                    HorizontalOptions="Center"
                    WidthRequest="300"/>-->
                    <SearchBar x:Name="SearchBar" Placeholder="Search activities by Location" TextChanged="OnSearchBarTextChanged" WidthRequest="300" />
                    <ListView x:Name="LocationSuggestionsListView" ItemsSource="{Binding Suggestions}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Label Text="{Binding .}" />
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                    <ImageButton Grid.Row="0" Source="ic_add.png" x:Name="Plus" Clicked="Plus_Clicked"
                     VerticalOptions="Center" HorizontalOptions="End" HeightRequest="35" Aspect="AspectFit" />

                    <!--<Picker x:Name="Location" HorizontalOptions="Center"
                 Title="Location"
                 ItemsSource="{Binding ItemsWithLocation}" ItemDisplayBinding="{Binding Location}"  />-->

                    <StackLayout Grid.Row="4" HeightRequest="600" HorizontalOptions="FillAndExpand" Margin="0">
                        <RefreshView Command="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
                            <CollectionView x:Name="ItemsListView"                ItemsSource="{Binding FilteredItems}"                SelectionMode="None">
                                <CollectionView.ItemTemplate>
                                    <DataTemplate>
                                        <StackLayout Padding="10" >
                                            <StackLayout.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="OnItemTapped" />
                                            </StackLayout.GestureRecognizers>
                                            <StackLayout Orientation="Horizontal">
                                                <Image Source="{Binding Image, Converter={StaticResource ToImage}}" 
                                                   HeightRequest="50" WidthRequest="70"/>
                                                <Label Text="{Binding Text}" LineBreakMode="WordWrap" 
                                                   Style="{DynamicResource ListItetextStyle}" FontSize="16" 
                                                   TextColor="Black"/>
                                            </StackLayout>

                                        </StackLayout>
                                    </DataTemplate>
                                </CollectionView.ItemTemplate>
                            </CollectionView>
                        </RefreshView>
                    </StackLayout>
                </Grid>
            </StackLayout>

        </ScrollView>

    </ContentPage.Content>

</ContentPage>

有两种方式可以显示此信息:把搜索栏放在代码的最顶端或删除此块中的代码ListView
我也试过设置背景颜色或文本颜色,但它不会改变任何东西。
第一变化

<StackLayout Orientation="Vertical">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="auto"/>
             <RowDefinition Height="*" />
             <RowDefinition Height="*" />
         </Grid.RowDefinitions>
         <BoxView Grid.Row="0" x:Name="Box" BackgroundColor="White" />
         <!--<SearchBar 
         HorizontalOptions="Center"
         WidthRequest="300"/>-->
         <SearchBar Grid.Row="1" x:Name="SearchBar" Placeholder="Search activities by Location" TextChanged="OnSearchBarTextChanged" WidthRequest="300" />
         <ListView Grid.Row="2" x:Name="LocationSuggestionsListView" ItemsSource="{Binding Suggestions}">
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <ViewCell>
                         <Label Text="{Binding .}" />
                     </ViewCell>
                 </DataTemplate>
             </ListView.ItemTemplate>
         </ListView>

wj8zmpe1

wj8zmpe11#

我可以重现SearchBar在Windows上正确显示的问题,但它无法在iOS上显示。
根据您的代码片段,您可能需要为SearchBar添加HeightRequest="50"属性,如下所示:

<Grid> 
       <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="50" />
              <RowDefinition Height="*" />                  
       </Grid.RowDefinitions>

      <BoxView Grid.Row="0" x:Name="Box" HeightRequest="100" Color="White" />            
      <SearchBar  Grid.Row="1" x:Name="SearchBar" Placeholder="Search activities by Location" WidthRequest="300" HeightRequest="50"/>
       <ListView  Grid.Row="2"  x:Name="LocationSuggestionsListView" ItemsSource="{Binding Suggestions}" >
               <ListView.ItemTemplate>
                    <DataTemplate>
                           <TextCell Text="{Binding .}"/>
                     </DataTemplate>
                </ListView.ItemTemplate>
       </ListView>
</Grid>

PS:

考虑将<TextCell Text="{Binding .}"/>用于ListView的DataTemplate

相关问题