Interaction with ARItems

Jun 18, 2013 at 3:00 AM
Edited Jun 18, 2013 at 3:06 AM
I'm trying to use GART for a game, and I am struggling to interact with the ARItems individually. For example I would like to click on a label and display information about that label, but how do I place an event listener on an ARItem specifically rather than the display?
Developer
Jun 18, 2013 at 7:56 AM
The way I've done this is by creating a custom DataTemplate for the WorldView which is used to render the ARItems with an event handler. Here is what my DataTemplate looks like:
<Page.Resources>
    <DataTemplate x:Key="PoiItem">
        <Border BorderBrush="Black" BorderThickness="2" CornerRadius="8" Background="#FF003847" Width="250">
            <Grid Margin="4" Tapped="PoiItem_Tapped">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="0.5*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                
                <Image Grid.RowSpan="3" Source="{Binding EntityTypeID, Converter={StaticResource EntityIconConverter}}" 
                       Stretch="Uniform" MaxHeight="50" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,10,0"/>
                
                <TextBlock Text="{Binding Name}" Grid.Column="1"/>
                <TextBlock Text="{Binding EntityTypeID, Converter={StaticResource EntityNameConverter}}" Grid.Row="1" Grid.Column="1"/>
                
                <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1">
                    <TextBlock Text="{Binding Distance}"/>
                    <TextBlock Text=" meters"/>
                </StackPanel>                    
            </Grid>
        </Border>
    </DataTemplate>
</Page.Resources>
In the Grid you can see I have added a Tapped event called PoiItem_Tapped. I've added this event handler to my code behind like this:
private void PoiItem_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
    ARItem selectedItem = null;

    if (sender is Grid)
    {
        selectedItem = (sender as Grid).DataContext as ARItem;

        //Do something with selected item
    }
}
Finally I add the data template to the WorldView like so:
<ARControls:WorldView x:Name="WorldView" ItemTemplate="{StaticResource PoiItem}"/>
Jun 18, 2013 at 8:49 AM
Oh sweet. DataTemplate was the trick. Thanks for the help!
Oct 13, 2016 at 12:16 PM
This works as well for me, but as soon as I have something which overlays the ARDisplay, the tap event is not fired anymore (if I click on an item shown in the AR-view).

Any ideas what goes wrong? Thanks!