添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
买醉的牛肉面  ·  Amazon.com: Cell ...·  1 周前    · 
腼腆的伏特加  ·  Python ...·  1 年前    · 
打盹的板栗  ·  xml - Expected ...·  1 年前    · 

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

Download Sample Download the sample

By default from Xamarin.Forms 4.3, when a ViewCell in an Android application defines context actions for each item in a ListView , the context actions menu is updated when the selected item in the ListView changes. However, in previous versions of Xamarin.Forms the context actions menu was not updated, and this behavior is referred to as the ViewCell legacy mode. This legacy mode can result in incorrect behavior if a ListView uses a DataTemplateSelector to set its ItemTemplate from DataTemplate objects that define different context actions.

This Android platform-specific enables the ViewCell context actions menu legacy mode, for backwards compatibility, so that the context actions menu is not updated when the selected item in a ListView changes. It's consumed in XAML by setting the ViewCell.IsContextActionsLegacyModeEnabled bindable property to true :

<ContentPage ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout Margin="20">
        <ListView ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell android:ViewCell.IsContextActionsLegacyModeEnabled="true">
                        <ViewCell.ContextActions>
                            <MenuItem Text="{Binding Item1Text}" />
                            <MenuItem Text="{Binding Item2Text}" />
                        </ViewCell.ContextActions>
                        <Label Text="{Binding Text}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

Alternatively, it can be consumed from C# using the fluent API:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
viewCell.On<Android>().SetIsContextActionsLegacyModeEnabled(true);

The ViewCell.On<Android> method specifies that this platform-specific will only run on Android. The ViewCell.SetIsContextActionsLegacyModeEnabled method, in the Xamarin.Forms.PlatformConfiguration.AndroidSpecific namespace, is used to enable the ViewCell context actions menu legacy mode, so that the context actions menu is not updated when the selected item in a ListView changes. In addition, the ViewCell.GetIsContextActionsLegacyModeEnabled method can be used to return whether the context actions legacy mode is enabled.

The following screenshots show ViewCell context actions legacy mode enabled:

In this mode, the displayed context action menu items are identical for cell 1 and cell 2, despite different context menu items being defined for cell 2.

The following screenshots show ViewCell context actions legacy mode disabled, which is the default Xamarin.Forms behavior:

In this mode, the correct context action menu items are displayed for cell 1 and cell 2.

  • PlatformSpecifics (sample)
  • Creating Platform-Specifics
  • AndroidSpecific API
  • AndroidSpecific.AppCompat API
  •