我在 collectionview 中有 stacklayout,我想點擊 stacklayout 元素來執行系結命令。我將 TapGestureRecognizer 添加到 stacklayout 的那一刻 - Stacklayout 停止回應觸摸(雖然我仍然可以滾動它)并且系結命令不會觸發:
<CollectionView ItemsSource="{Binding ResponseResponses}" SelectionMode="Single" >
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding SearchCommand}" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
<Label Text="{Binding Username, StringFormat=User: {0}}" />
<Label Text="{Binding FileCount, StringFormat=Files: {0}}" />
<BoxView HeightRequest="1"
BackgroundColor="LightGray"
VerticalOptions="End"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
因此,如果沒有 GestureRecognizers - 對 stacklayout 元素的選擇有效,而 - 則無效。如果我將 GestureRecognizers 添加到標簽(例如),則該標簽上的觸摸會被阻止。
順便說一句,{Binding SearchCommand} 是自己作業的,它系結到視圖中的其他按鈕,所以可能沒有原因。
那么,我在這里做錯了什么?
uj5u.com熱心網友回復:
所以,正如我在這里找到的: CollectionView Tips
請記住,在 ItemTemplate 中作業時,BindingContext 是專案本身,而不是 ContentPage 的專案本身。如果您的命令存在于該專案上,那么您已準備就緒。但是,如果您希望將此系結路由到 ContentPage 視圖模型上的命令,如本示例中所示,您可以使用新支持的 RelativeSource。
所以你必須使用這樣的系結:
<CollectionView.ItemTemplate>
<DataTemplate>
<views:ResultViewA>
<views:ResultViewA.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding
Source={RelativeSource
AncestorType={x:Type vm:FlightResultsViewModel}},
Path=GoToDetailsCommand}"
CommandParameter="{Binding .}"/>
</views:ResultViewA.GestureRecognizers>
</views:ResultViewA>
</DataTemplate>
</CollectionView.ItemTemplate>
就我而言,它現在看起來像這樣:
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding
Source={RelativeSource
AncestorType={x:Type viewmodels:SearchViewModel}},
Path=SearchCommand}"
CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
<Label Text="{Binding Username, StringFormat=User: {0}}"/>
<Label Text="{Binding FileCount, StringFormat=Files: {0}}"/>
<BoxView HeightRequest="1"
BackgroundColor="LightGray"
VerticalOptions="End" InputTransparent="True"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334658.html
標籤:C# 。网 沙马林 xamarin.forms 安卓系统
