0
votes

I am having an issue with my Commmand inside of a ViewCell in a ListView. My Data Template for the List view is currently using Bindings to get multiple object data from a JSON string and I have a feeling that this is preventing my Command Binding from being accessed in my XAML here

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MIApp.HomePage">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label Text="News Articles"
                    VerticalOptions="CenterAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
                <ListView x:Name="GetListView" HasUnevenRows="true">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>  
                                <StackLayout VerticalOptions="FillAndExpand" Margin="0,20,0,20">
                                    <StackLayout.GestureRecognizers>
                                        <TapGestureRecognizer
                                            Command="{Binding ArtCommand}"
                                            NumberOfTapsRequired="1"
                                            />
                                    </StackLayout.GestureRecognizers>
                                    <Label Text="{Binding strArticleTitle}"/>
                                    <Image Source="{Binding strArticlePhotoUrl}"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

In my code behind I set the binding context to the view model here as well as retrieve the data for my list view.

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {

        HomeVM viewModel;

        public HomePage()
        {
            InitializeComponent();

            viewModel = new HomeVM();
            BindingContext = viewModel;
        }



        protected async override void OnAppearing()
        {
            base.OnAppearing();

            HttpClient client = new HttpClient();
            string url = "https://example.net/api/Articles/GetArticles";
            var response = await client.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                string res = "";
                using (HttpContent content = response.Content)
                {
                    Task<string> result = content.ReadAsStringAsync();
                    res = result.Result;
                    var ArticlesList = Articles.ArticlesItems.FromJson(res);
                    GetListView.ItemsSource = ArticlesList;
                }
            }
            else
            {
                await DisplayAlert("Connection Error", "Please Connect to the internet and try again", "Ok");
            }
        }

    }

Is there any way to get around this, I'm new to Xamarin Forms.

Thanks,

Ryan

1

1 Answers

1
votes

The DataTemplate context of the list view is bound to the source item, This is where it expects the command.

However you can do something like this if you want you Command in the main view model for the page.

<ContentPage
   ...
   x:Name="This"

And your DataTemplate

<TapGestureRecognizer
     Command="{Binding Path=BindingContext.ArtCommand, Source={x:Reference This}}

However you'll have to set the CommandParameter="{Binding}" to get the item

On Saying all that, you could also use ItemSelected and ItemTapped with code behind, or EventToCommand behavior