У меня проблема с моей Командой внутри ViewCell в ListView. Мой шаблон данных для представления «Список» в настоящее время использует привязки для получения данных нескольких объектов из строки JSON, и у меня есть ощущение, что это препятствует доступу к моей привязке команды в моем XAML здесь
<?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>
В своем коде я установил здесь контекст привязки для модели представления, а также извлек данные для моего представления списка.
[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");
}
}
}
Есть ли способ обойти это, я новичок в формах Xamarin.
Спасибо
Ryan