You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI iOS平台下如何修改SwipeItem的FontSize?

.NET MAUI iOS平台修改SwipeItem字体大小的方案探讨

问题

在.NET MAUI的iOS平台中,是否有办法修改SwipeItem的FontSize?目前未找到可用的设置项,尝试通过资源设置也无效。

自定义SwipeItem的尝试及问题

尝试通过SwipeItemView自定义SwipeItem,但关闭后存在显示弊端,虽可设置高度,但与默认高度显示效果存在差异。相关XAML代码如下:

<SwipeItems>
    <SwipeItemView>
        <Grid RowDefinitions="*" ColumnDefinitions="*, *" WidthRequest="150" ColumnSpacing="0">
            
            <Label Grid.Column="1" Text="{loc:Translate ItemsOverview_SwipeItemEditItem}" BackgroundColor="LightGrey" Style="{StaticResource SwipeItemStyle}">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:ItemsOverviewViewModel}}, Path=EditItemCommand}" CommandParameter="{Binding .}"/>
                </Label.GestureRecognizers>
            </Label>

            <Label Grid.Column="0" Text="{loc:Translate ItemsOverview_SwipeItemDeleteItem}" BackgroundColor="{StaticResource DarkOrange1}" TextColor="White" Style="{StaticResource SwipeItemStyle}">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:ItemsOverviewViewModel}}, Path=DeleteItemCommand}" CommandParameter="{Binding .}"/>
                </Label.GestureRecognizers>
            </Label>
            
        </Grid>
    </SwipeItemView>
</SwipeItems>

关闭自定义SwipeItem后的显示效果:布局高度与默认SwipeItem存在明显差异,显示不符合预期。

更新内容:自定义SwipeView实现关闭逻辑

为处理SwipeItem关闭后的布局问题,实现了自定义CustomSwipeView:

public class CustomSwipeView : SwipeView
{
    public static readonly BindableProperty OpenFlagProperty = BindableProperty.Create(nameof(OpenFlag), typeof(bool), typeof(CustomSwipeView), true, propertyChanged: OnPropertyChanged);

    private static void OnPropertyChanged(BindableObject sender, object oldValue, object newValue)
    {
        SwipeView swipeView = sender as SwipeView;
        var openSwipeViewFlag = (bool)newValue;
        
        if (swipeView != null)
        {
            if (openSwipeViewFlag == true)
            {
                swipeView.Close();

                // 关闭后将flag重置为false,恢复初始状态
                ((CustomSwipeView)sender).OpenFlag = false;
            }
        }
    }

    public bool OpenFlag
    {
        get { return (bool)GetValue(OpenFlagProperty); }
        set { SetValue(OpenFlagProperty, value); }
    }
}

ViewModel相关代码

private bool _openFlag;
public bool OpenFlag
{
    get
    {
        return _openFlag;
    }

    set
    {
        _openFlag = value;

        // 仅在值发生变化时触发属性变更通知(如false→true)
        OnPropertyChanged(nameof(OpenFlag));
    }
}

[RelayCommand]
private async Task EditItem(Item item)
{
    OpenFlag = true;

    // 其他编辑逻辑...
}

完整XAML布局代码

<!-- 背景图与渐变层 -->
<Grid Padding="0, 0, 0, -25">

   <views:BackgroundImageView x:Name="BackgroundImage" VerticalOptions="Start"/>

   <Border Style="{StaticResource BorderStyleView}" VerticalOptions="FillAndExpand">

       <Grid RowDefinitions="Auto, *">
           
           <!-- 搜索栏 -->
           <SearchBar Grid.Row="0" ios:SearchBar.SearchBarStyle="Minimal" Text="{Binding SearchText}" Placeholder="{loc:Translate ItemsOverview_SearchItemPlaceholder}"/>

           <!-- 集合视图:调整边距使滚动条靠近右侧边框 -->
           <CollectionView Grid.Row="1" ItemsSource="{Binding GroupedItems, Mode=TwoWay}" Style="{StaticResource CollectionViewStyle}" x:Name="MyCollectionView">

               <!-- 分组头部模板 -->
               <CollectionView.GroupHeaderTemplate>
                   <DataTemplate x:DataType="viewModels:GroupedItemList">
                       <VerticalStackLayout>
                           <Label Text="{Binding GroupName, StringFormat=' {0}'}" Style="{StaticResource GroupHeaderStyle}" />
                           <BoxView Style="{StaticResource GroupHeaderUnderlineStyle}"/>
                       </VerticalStackLayout>
                   </DataTemplate>
               </CollectionView.GroupHeaderTemplate>

               <!-- 分组项模板 -->
               <CollectionView.ItemTemplate>
                   <DataTemplate x:DataType="{x:Type databaseModels:Item}">

                       <controls:CustomSwipeView x:Name="MySwipeView" OpenFlag="{Binding OpenFlag, Source={RelativeSource AncestorType={x:Type viewModels:ItemsOverviewViewModel}}, Mode=TwoWay}">

                           <!-- 带圆角矩形的项名称 -->
                           <Grid Padding="0, 5">
                               <Frame>
                                   <Label Text="{Binding Name}" FontAttributes="Bold" />
                               </Frame>
                           </Grid>

                           <SwipeView.RightItems>
                               
                               <SwipeItems>
                                   <SwipeItemView>
                                       <Grid RowDefinitions="*" ColumnDefinitions="*, *" WidthRequest="160" ColumnSpacing="0">
                                           
                                           <Label Grid.Column="1" Text="{loc:Translate ItemsOverview_SwipeItemEditItem}" BackgroundColor="LightGrey" Style="{StaticResource SwipeItemStyle}">
                                               <Label.GestureRecognizers>
                                                   <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:ItemsOverviewViewModel}}, Path=EditItemCommand}" CommandParameter="{Binding .}"/>
                                               </Label.GestureRecognizers>
                                           </Label>

                                           <Label Grid.Column="0" Text="{loc:Translate ItemsOverview_SwipeItemDeleteItem}" BackgroundColor="{StaticResource DarkOrange1}" TextColor="White" Style="{StaticResource SwipeItemStyle}">
                                               <Label.GestureRecognizers>
                                                   <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:ItemsOverviewViewModel}}, Path=DeleteItemCommand}" CommandParameter="{Binding .}"/>
                                               </Label.GestureRecognizers>
                                           </Label>
                                           
                                       </Grid>
                                   </SwipeItemView>
                               </SwipeItems>
                               
                               
                           </SwipeView.RightItems>
                       </controls:CustomSwipeView>

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

   </Border>

</Grid>

内容的提问来源于stack exchange,提问作者OXO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 14:59:52