代码之家  ›  专栏  ›  技术社区  ›  Phill

从viewcell内的图像中从tapgesture访问列表数据

  •  0
  • Phill  · 技术社区  · 8 年前

    我在一个 ViewCell TapGestureRecognizer 对于图像,现在当用户单击图像时,我想从数据中访问数据 可视单元格

    2 回复  |  直到 8 年前
        1
  •  2
  •   Pratius Dubey    8 年前

    这是在ViewCell上传递数据的简单代码。

    代码用户界面--

    <ListView ItemsSource="{Binding ListItem}" x:Name="lst">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                  <StackLayout>
                   <Image Source="abcd" Aspect="AspectFit">
                   <Image.GestureRecognizers>
                   <TapGestureRecognizer Command="{Binding ClickCommand}" 
                          Source={x:Reference lst}}" 
                          CommandParameter="{Binding .}" />
                     </Image.GestureRecognizers>
                     </Image>
                   </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

     Public Class MainViewModel
    {
       Command clickCommand;
        public Command ClickCommand
        {
            get
            {
           return clickCommand ?? (menuTapCommand = new Command<Object>(GetImage));
            }
        }
    Private Void GetImage(Object obj)
    {
     //Todo
    }}
    
        2
  •  0
  •   TaylorD    8 年前

    创建 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
        }
    }
    
    推荐文章