如何在Xamarin.Forms框架内填充和展开按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Xamarin.Forms框架内填充和展开按钮相关的知识,希望对你有一定的参考价值。

我想将不同的Button放入<Frame>控件中,所有这些都是为了避免<ListView>中的图像溢出,但是将Horizo​​ntalOptions和VerticalOptions属性放入FillAndExpand时不会填充相框,我附上问题图片

screenshot

如何使<Button>内部的<Frame>充满空间?我尝试为“列和行”赋予“自动”属性,但没有成功。

如何保持按钮的大小(高度和宽度)但又不丢失框架?如何填充和展开框架内的按钮?

对我有帮助吗?

VIEW.XAML:

<ListView 
                ItemsSource="{Binding ListaInventario}"
                SelectionMode="None"
                VerticalOptions="FillAndExpand"
                IsRefreshing="{Binding IsRefreshing, Mode=TwoWay}"               
                HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout
                                Margin="0,4,0,0"
                            Orientation="Vertical">

                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="5*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                         <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>

                                    <Label
                                        Text="{Binding Id}"                                    
                                        FontSize="Small"                 
                                        HorizontalOptions="Center"
                                        FontAttributes="Bold"
                                        HeightRequest="39"
                                        VerticalTextAlignment="Start"                                    
                                        TextColor="{StaticResource das.color.texto}"                                     
                                        Grid.Column="0"
                                        Grid.Row="0">
                                    </Label>

                                    <Label                                 
                                        Text="{Binding NombreComercial}"
                                        HorizontalOptions="Start"
                                        FontSize="Small" 
                                        MaxLines="3"
                                        HeightRequest="39"
                                        TextColor="{StaticResource das.color.texto}"
                                        VerticalOptions="Start"
                                        Grid.Column="1"
                                        Grid.Row="0">
                                    </Label>


                                    <Frame
                                        HorizontalOptions="End"
                                        BackgroundColor="Transparent"
                                        Grid.Column="2"
                                        Grid.Row="0">

                                         <Button 
                                        ImageSource="ic_pdf"
                                        HeightRequest="35"
                                        WidthRequest="50"
                                        BackgroundColor="{StaticResource das.color.estado_primary}"                                                  
                                        HorizontalOptions="FillAndExpand"
                                        VerticalOptions="FillAndExpand"                                          
                                        Command="{Binding HDSSisquimCommand}"/>

                                    </Frame>




                                </Grid>

                                <!---->

                                        <!--CANTIDAD-->
                                    <StackLayout
                                        Orientation="Horizontal"
                                        HorizontalOptions="FillAndExpand"
                                        Margin="0,1,0,2">

                                        <StackLayout               
                                              Orientation="Horizontal"
                                              HorizontalOptions="Start"
                                              Margin="0,0,3,0">
                                            <Label
                                                HorizontalOptions="Start"  
                                                HorizontalTextAlignment="Start"
                                                VerticalOptions="End"
                                                VerticalTextAlignment="Center"
                                                Text="Cantidad: "
                                                FontAttributes="Bold"
                                                FontSize="Small"
                                                HeightRequest="35"                    
                                                IsEnabled="{Binding EnabledEstimadoEntry}"></Label>
                                            <Entry
                                                HorizontalOptions="FillAndExpand"
                                                Placeholder="Cantidad"
                                                Keyboard="Numeric"
                                                HorizontalTextAlignment="Center"
                                                MaxLength="4"
                                                FontSize="Small"                                                   
                                                HeightRequest="35"
                                                Text="{Binding CantidadDecimal}"
                                                IsEnabled="{Binding EnabledContenedorEntry}">
                                                <VisualStateManager.VisualStateGroups>
                                                  <VisualStateGroup x:Name="CommonStatesEntry">
                                                  <VisualState x:Name="Normal">
                                                   <VisualState.Setters>
                                                     <Setter Property="BackgroundColor" Value="Transparent" />
                                                   </VisualState.Setters>
                                                 </VisualState>
                                                 <VisualState x:Name="Focused">
                                                    <VisualState.Setters>
                                                        <Setter Property="BackgroundColor" Value="LightGray" />
                                                    </VisualState.Setters>
                                                </VisualState>
                                                </VisualStateGroup>
                                              </VisualStateManager.VisualStateGroups>
                                            </Entry>
                                        </StackLayout>


                                        <!--UNIDAD-->
                                        <StackLayout             
                                            Orientation="Horizontal"
                                            HorizontalOptions="FillAndExpand">
                                            <Label
                                                HorizontalOptions="Start"  
                                                HorizontalTextAlignment="Start"
                                                VerticalOptions="End"
                                                VerticalTextAlignment="Center"
                                                Text="Unidad: "                                                    
                                                FontAttributes="Bold"
                                                FontSize="Small"
                                                HeightRequest="35"                    
                                                IsEnabled="{Binding EnabledEstimadoEntry}"></Label>
                                            <Picker 
                                                iosSpecific:Picker.UpdateMode = "WhenFinished"
                                                ItemsSource="{Binding ListaUnidadesMedida}"
                                                SelectedItem="{Binding SelectedUnidadMedida}"
                                                ItemDisplayBinding="{Binding NombreCorto}"
                                                Title="Unidad"   
                                                FontSize="Small"
                                                HeightRequest="35"
                                                HorizontalOptions="FillAndExpand"
                                                IsEnabled="{Binding EnabledContenedor}">
                                                <VisualStateManager.VisualStateGroups>
                                                  <VisualStateGroup x:Name="CommonStatesPicker">
                                                      <VisualState x:Name="Normal">
                                                   <VisualState.Setters>
                                                     <Setter Property="BackgroundColor" Value="Transparent" />
                                                   </VisualState.Setters>
                                                 </VisualState>
                                                 <VisualState x:Name="Focused">
                                                    <VisualState.Setters>
                                                        <Setter Property="BackgroundColor" Value="LightGray" />
                                                    </VisualState.Setters>
                                                </VisualState>
                                                 </VisualStateGroup>
                                             </VisualStateManager.VisualStateGroups>
                                            </Picker>
                                        </StackLayout>


                                    </StackLayout>

                                <!-- TWO BUTTONS -->
                                <StackLayout
                                     Orientation="Horizontal"
                                        HorizontalOptions="End"
                                        Margin="0,1,0,2">

                                                <Button
                                                    Margin="0,0,2,0"
                                            ImageSource="ic_actualizar"                                              
                                            BackgroundColor="{Binding ColorBotonActualizar}"                                                       
                                            HorizontalOptions="End"
                                            VerticalOptions="Center"
                                            HeightRequest="35"
                                            WidthRequest="50"
                                            Command="{Binding ActualizarSQCommand}"/>

                                               <Button                                              
                                            ImageSource="ic_minus_sisquim"                                              
                                            BackgroundColor="{StaticResource das.color.estado_danger}"                                                       
                                            HorizontalOptions="End"
                                            VerticalOptions="Center"
                                            HeightRequest="35"
                                            WidthRequest="50"
                                            Command="{Binding DesasociarSQCommand}"/>

                                </StackLayout>

                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
答案
您可以尝试将属性Padding="0"添加到Frame并删除以下Button的属性。

HeightRequest="35" WidthRequest="50"

所以代码是这样的:

<Frame HorizontalOptions="End" BackgroundColor="Green" Grid.Column="2" Padding="0" Grid.Row="0"> <Button Text="test" BackgroundColor="Accent" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" /> </Frame>

以上是关于如何在Xamarin.Forms框架内填充和展开按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Xamarin Forms 中按顺序显示(出现)元素

Xamarin.Forms Button长按事件

使用 Xamarin.Forms 应用程序填充 iPhone X 屏幕

使用Xamarin.Forms应用程序填充iPhone X屏幕

Xamarin.forms如何在列表中显示彼此相邻的项目

Xamarin Forms - 在页面打开时填充自定义控件