Skip to main content
 首页 » 编程设计

c#之如何在 Xamarin.Forms ListView 下添加按钮

2024年02月05日15lexus

你好,我有一个应用程序,我正在用 xamrian 形式做,我有一个卡片样式的 ListView 。我希望能够在 ListView 下方添加 3 个按钮,一个在左侧,一个在中间,一个在右侧 这是我的 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" 
             x:Class="App.HomePage"> 
  <ListView x:Name="listView" HasUnevenRows="true"  ItemSelected="OnItemSelected"> 
    <ListView.ItemTemplate> 
      <DataTemplate> 
        <ViewCell> 
          <Frame Padding="0,0,0,8" BackgroundColor="#d2d5d7"> 
            <Frame.Content> 
              <Frame Padding="15,15,15,15"   OutlineColor="Gray" BackgroundColor="White"> 
                <Frame.Content> 
                  <StackLayout Padding="20,0,0,0"  Orientation="Horizontal" HorizontalOptions="CenterAndExpand"> 
                    <Image 
                          HorizontalOptions="StartAndExpand" 
                          Source="{Binding Image}" /> 
                    <Label Text="{Binding Name}" 
                           TextColor="#69add1" 
                           FontFamily="OpenSans-Light" 
                           FontSize="24"/> 
                  </StackLayout> 
                </Frame.Content> 
              </Frame> 
            </Frame.Content> 
          </Frame>   
 
          </ViewCell> 
      </DataTemplate> 
    </ListView.ItemTemplate> 
  </ListView> 
  </ContentPage> 

我将如何在 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:local="clr-namespace:Sandbox_Forms" 
             x:Class="Sandbox_Forms.MainPage"> 
 
  <Grid> 
    <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
 
    <ListView Grid.Row="0" BackgroundColor="Aqua"> 
      <!-- Add bindings and data template --> 
    </ListView> 
    <StackLayout Orientation="Horizontal" Grid.Row="1"> 
      <Button Text="Button1" HorizontalOptions="FillAndExpand"/> 
      <Button Text="Button2" HorizontalOptions="FillAndExpand"/> 
      <Button Text="Button3" HorizontalOptions="FillAndExpand"/> 
    </StackLayout> 
  </Grid> 
 
</ContentPage> 

这将限制您的 ListView 仅填充您的按钮不占用的空间。输出应如下所示: