gpt4 book ai didi

xamarin - 如何在 Xamarin.Forms 中禁用 ListView 上的突出显示

转载 作者:行者123 更新时间:2023-12-03 07:56:26 25 4
gpt4 key购买 nike

我正在使用 Xamarin.Forms 和 XAML,并且我正在尝试创建一个存储产品列表的应用程序。我将我的产品列表放在 ListView 中。这工作正常。这是我的 XAML:

<ListView x:Name="listSushi"
ItemsSource="{x:Static local:myListSushi.All}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
RowHeight="{StaticResource rowHeight}"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="5, 5, 0, 5"
Orientation="Horizontal"
Spacing="15">
<StackLayout>
<Image Source="{Binding ImageSource}" />
</StackLayout>

<StackLayout Padding="0, 0, 0, 0"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand">
<Label Text="{Binding Name}"
Font="Bold, Medium" />
<Label Text="{Binding Description}"
Font="Small"/>
</StackLayout>

<StackLayout Orientation="Horizontal"
Padding="0, 0, 10, 0">
<Button Text=" - "
HorizontalOptions="EndAndExpand"
VerticalOptions="FillAndExpand"
Command="{Binding DeleteSushiCommand}"
CommandParameter="{Binding Name}"
/>
<Label VerticalOptions="Center"
Text="{Binding Number,StringFormat='{0}'}"
TextColor="Black"/>
<Button Text=" + "
HorizontalOptions="EndAndExpand"
VerticalOptions="FillAndExpand"
Command="{Binding AddSushiCommand}"
CommandParameter="{Binding Name}"
/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>

我只是有一个问题,如果我点击我的 listView 的一个单元格,该单元格会突出显示,并保持突出显示。我尝试在 xaml.cs 中使用此代码禁用它

listSushi.ItemSelected+= (object sender, SelectedItemChangedEventArgs e) => {
// don't do anything if we just de-selected the row
if (e.SelectedItem == null) return;
// do something with e.SelectedItem
((ListView)sender).SelectedItem = null; // de-select the row
};

但是当我触摸一个单元格时,现在我的列表会自动滚动。这很奇怪。

有谁知道这是一个错误,或者知道一个修复程序,比如是否有一个属性可以禁用突出显示?

最佳答案

您可以尝试改用 ItemTapped 事件,即

listSushi.ItemTapped += (object sender, ItemTappedEventArgs e) => {
// don't do anything if we just de-selected the row.
if (e.Item == null) return;

// Optionally pause a bit to allow the preselect hint.
Task.Delay(500);

// Deselect the item.
if (sender is ListView lv) lv.SelectedItem = null;

// Do something with the selection.
...
};

我已经在 ListView(在 Android 设备上)上测试了这个,它有足够的项目可以将滚动引入混合。我没有看到自动滚动行为,并且您将 SelectedItem 设置为 null 以消除突出显示的想法非常有效。

关于xamarin - 如何在 Xamarin.Forms 中禁用 ListView 上的突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com