Jono_2007
Jono_2007

Reputation: 1046

Xamarin.Forms CollectionView trigger event on Focus/Hover

I'm currently attempting to implement a Xamarin.Forms application which will be targeting Android TV. I have simplified my application to show the specific issue I'm having.

I have a collection of items which which I am displaying via a CollectionView. I am able to scroll around items inside this CollectionView using the D-Pad controls absolutely fine, as can be seen via this screenshot, where the "focussed"/"selected" item is visible if I make the layout bounds visible. Selected Item Visible

There is nothing available except the "ItemSelected" event on the CollectionView which only occurs on actually pressing enter. I have tried to add in "Focus" events to all the elements that make up this page and nothing gets triggered. I am unsure what exactly is being "focussed" upon here.

This is my page:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:viewmodels="clr-namespace:Channels.ViewModels" 
             x:Class="Channels.MainPage"
             x:DataType="viewmodels:ChannelViewModel"
             BackgroundColor="{DynamicResource BackgroundColour}">
    <StackLayout Orientation="Vertical" Padding="10">
        <Label Text="Selected Item" TextColor="White" Margin="0, 0, 10, 10"></Label>
        <CollectionView ItemsSource="{Binding Data}">
            <CollectionView.ItemsLayout>
                <LinearItemsLayout ItemSpacing="5"></LinearItemsLayout>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Padding="5" Spacing="5" BackgroundColor="Red">
                        <Label Text="Test Text" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" TextColor="White"></Label>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage>

I would like to know exactly what is getting focus here and if it is possible to do something like I have stated.

I haven't found many examples of Xamarin being used in terms of Android TV so it might not be possible to do things like this. I have also tried implementing the same code in .NET Maui and cannot find any events that might work here.

Upvotes: 2

Views: 1200

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10978

You could use the GestureRecognizers in the template to triger what you want to do when you selected the item or click on the item.

   <Label>
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
            </Label.GestureRecognizers>
        </Label>

Upvotes: 0

Related Questions