xamarin 无法使用命令NO EVENT在CollectionView中两次选择同一项

b0zn9rqh  于 2023-04-18  发布在  其他
关注(0)|答案(3)|浏览(144)

我不能选择一个项目两次。我不使用事件。我用命令和ViewModel管理一切。我只能找到代码隐藏的解决方案。
CodeBehind Solution((CollectionView)sender).SelectedItem = null;
我不使用发送器,所以这个变体不起作用。
下面是我的CommandCall:

<CollectionView SelectionMode="Single"
                ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}}, 
                                       Path=UserPartyById}"
                ItemTemplate="{StaticResource Profile_Party_DataTemplate}"
                SelectedItem="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}}, 
                                      Path=SelectedItem}"

                SelectionChangedCommand="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}}, 
                                      Path=GoPartyDetailCommand}">

    <CollectionView.ItemsLayout>
        <LinearItemsLayout ItemSpacing="5" Orientation="Horizontal" />
    </CollectionView.ItemsLayout>

</CollectionView>
GoPartyDetailCommand = new Command(GoPartyDetail);

private Merged_Model selectedItem;
public Merged_Model SelectedItem
{
    get => selectedItem;
    set => SetPorperty(ref selectedItem, value);
}

public async void GoPartyDetail()
{
    Preferences.Set("SelectPartyId", SelectedItem.Id);
    Preferences.Set("FK_User", SelectedItem.FK_User);
    await _navigationService.NavigateToAsync<PartyDetail_ViewModel>();
}

H

mnowg1ta

mnowg1ta1#

SelectionChanged的预期行为是正确的-如果您选择同一项两次,它不会被触发,因为selectedItem已经被选中。触发SelectionChanged的唯一方法是(1)在再次选择项之前选择不同的项,或者(2)将选定项设置为null,但在将其设置为null之前需要一些延迟。我尝试了500毫秒,它工作了。
请参考链接https://github.com/xamarin/Xamarin.Forms/issues/11405

await Task.Delay(5000); 
RecommandedAttendeesList.SelectedItem = null;
thtygnil

thtygnil2#

正如Maverick所说,你可以将SelectedItem设置为null,但在我的例子中,只要我将其设置为null,它就会再次触发SelectionChanged事件。我的解决方案(使用MVVM,但也应该与代码隐藏一起使用)是做一个null检查,如果是,则返回。

private async void ExecuteSelectionChanged(object item)
        {
            if(SelectedItem == null){ return; }
              [ your code logic ]
            SelectedItemLog = null;
        }
enyaitl3

enyaitl33#

您可以使用SelectionChangedCommandParameterColletionView传递给视图模型,然后可以使用clean the SelectedItem

<CollectionView
     x:Name = "collectionview"
     SelectionMode="Single"
     ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}}, 
                               Path=UserPartyById}"
     ItemTemplate="{StaticResource Profile_Party_DataTemplate}"
     SelectedItem="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}}, 
                              Path=SelectedItem}"

     SelectionChangedCommand="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=GoPartyDetailCommand}"   
     SelectionChangedCommandParameter="{x:Reference collectionview}"
        >

        <CollectionView.ItemsLayout>
            <LinearItemsLayout ItemSpacing="5" Orientation="Horizontal" />
        </CollectionView.ItemsLayout>

</CollectionView>

然后在视图模型中:

public ICommand GoPartyDetailCommand => new Command<CollectionView>(GoPartyDetail);

public async void GoPartyDetail(CollectionView collectionview)
{
   // you could get the collectionview,then clean the selectItem
    Preferences.Set("SelectPartyId", SelectedItem.Id);
    Preferences.Set("FK_User", SelectedItem.FK_User);
    await _navigationService.NavigateToAsync<PartyDetail_ViewModel>();

}

相关问题