Stacklayout中的Xamarin Listview在iOS上显示不正确

本文关键字:显示 不正确 iOS 中的 Xamarin Listview Stacklayout | 更新日期: 2023-09-27 17:54:48

我有以下xaml

      <StackLayout Orientation="Vertical"  VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand" Margin="0,0,0,0">
        <Label Text="Menu" />
        <ListView x:Name="lvMenu" ItemSelected="lvMenu_ItemSelected">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ImageCell ImageSource="{Binding ImageSource}" Text="{Binding TitleText}" />
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
      </StackLayout>

下面的c#填充它

    private void PopulateMenu()
    {
        var menu = new List<SettingItem>()
        {
            new SettingItem() { ImageSource="icon.png", TitleText="Show All Jobs", TargetPageType = typeof(JobListPage)  },
            new SettingItem() { ImageSource="opportunities.png", TitleText="Sync Tasks", TargetPageType = typeof(SyncPage)  },
            new SettingItem() { ImageSource="leads.png", TitleText="Advanced Settings", TargetPageType = typeof(SettingsPage)  },
            new SettingItem() { ImageSource="contacts.png", TitleText="About ", TargetPageType = typeof(AboutPage)  },
        };
        lvMenu.ItemsSource = menu;
    }

这一切都很好,但当它显示时,我得到

 Menu
   Show All Jobs
   Sync Tasks
   Advanced Settings
在iOS模拟器上

和7个空白行

Stacklayout中的Xamarin Listview在iOS上显示不正确

iOS上ListView的默认行为是添加空行。

为了克服这个问题,你需要将ListView封装在StackPanel中,

或创建自定义渲染:

protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
     base.OnElementChanged(e);
     if (this.Control == null) return;
     this.Control.TableFooterView = new UIView();
}

来源:Xamarin