创建
DataTemplate
对于XF ViewCell
BindingContext
在那张图像和细胞本身就是
数据
可视单元格
图像识别器。它正确地执行
ICommand
在我的
对象,其中包含所需的数据。你需要一个
i命令
<ListView ItemsSource="{Binding ListItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image Source="abcd">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ClickCommand}" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
数据
ListView的对象应具有
i命令
public class MyDataObject
{
private Action<string> _openUrl;
private string _url;
public MyDataObject(string url, Action<string> openUrl)
{
_url = url;
_openUrl = openUrl;
ClickCommand = new Command(AccessOtherData);
}
public ICommand ClickCommand { get; }
// Other data properties and such
private void AccessOtherData()
{
// Call method to open _url
_openUrl(_url);
}
}
ObservableCollection
数据
物体
public class ViewModel
{
public event EventHandler<string> OpenUrl;
// List filled with the MyDataObject
public ObservableCollection<MyDataObject> Collection { get; set; }
private void FillCollection()
{
// Create all MyDataObjects here
// Each object will be created like this
Collection.Add(new MyDataObject("insert url here", OpenUrlFromObject));
}
private void OpenUrlFromObject(string url)
{
OpenUrl?.Invoke(this, url); // Subscribe to this event in your Page and open
}
}