简体   繁体   English

在ListView内部的ViewCell内检测标签上的点击

[英]Detecting tap on label, inside ViewCell, inside ListView

I'm trying to handle something similar (from UI perspective), to: 我正在尝试处理类似的东西(从UI角度来看),以: 在此输入图像描述

in order to invoke two different business logics for: 为了调用两种不同的业务逻辑:

  • tapping at ViewCell element itself (inside ListView) - in example navigate to different page 点击ViewCell元素本身(在ListView内部) - 在示例中导航到不同的页面
  • tapping at Label element ( Clickable Label ), which is inside given ViewCell element - in example delete given object or smth else 点击标签元素(可点击标签 ),它在给定的ViewCell元素内 - 在示例中删除给定对象或其他

I would like to have whole "tapping" logic inside page ViewModel . 我希望在ViewModel页面中有完整的“点击”逻辑。

Based on Xamarin forum proposes, I'm able to invoke some logic of "tapping" my delete action from cell, however directly inside my data model - which in my PoV is not good solution, as I would like to manipulate my List collection (so the most preferable way, would be to have this logic at page ViewModel ). 根据Xamarin论坛的建议,我能够调用一些逻辑从单元格“点击”我的删除操作,但是直接在我的数据模型中 - 这在我的PoV中不是很好的解决方案,因为我想操纵我的List集合(所以最好的方法是在ViewModel页面上有这个逻辑。

What I have right now: 我现在拥有的:

My page View XAML code looks like: 我的页面查看XAML代码如下所示:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App.Views.TestView">
  <ContentPage.Content>
    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <ListView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" ItemsSource="{Binding MyItemsCollection}" SelectedItem="{Binding SelectedItem}">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout Orientation="Horizontal">

                  <!-- Name Label -->
                  <Label Text="{Binding Name}" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand" />

                  <!-- Delete "Icon" -->
                  <Label Text="Clickable Label" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand">
                    <Label.GestureRecognizers>
                      <TapGestureRecognizer Command="{Binding OnClickableLabel}" CommandParameter="{Binding .}" />
                    </Label.GestureRecognizers>
                  </Label>

                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

My page View C# code looks like (not specific code there, except binding **BindingContext* to page ViewModel): 我的页面查看C#代码看起来像(不是特定的代码,除了绑定** BindingContext *到页面ViewModel):

public partial class TestView : ContentPage
{
    public TestView()
    {
        InitializeComponent();
        BindingContext = ServiceLocator.Current.GetInstance<TestViewModel>();
    }
}

My page ViewModel C# code looks like: 我的页面ViewModel C#代码如下所示:

public class TestViewModel : ViewModelBase
{
    public TestViewModel()
    {
        MyItemsCollection = GetMyItemsCollection();
    }

    private List<MyItem> GetMyItemsCollection()
    {
        return new List<MyItem>
        {
            new MyItem
            {
                ID = 1L,
                Name = "Item 1 Name"
            },
            new MyItem
            {
                ID = 2L,
                Name = "Item 2 Name"
            },
            new MyItem
            {
                ID = 3L,
                Name = "Item 3 Name"
            }
        };
    }

    private List<MyItem> _myItemsCollection { get; set; }

    public List<MyItem> MyItemsCollection
    {
        get
        {
            return _myItemsCollection;
        }
        set
        {
            _myItemsCollection = value;
            RaisePropertyChanged();
        }
    }

    private MyItem _SelectedItem { get; set; }

    public MyItem SelectedItem
    {
        get
        {
            return _SelectedItem;
        }
        set
        {
            if (_SelectedItem != value)
            {
                _SelectedItem = value;
                RaisePropertyChanged();

                Debug.WriteLine("SelectedItem: " + _SelectedItem.Name);
            }
        }
    }

    private RelayCommand<object> _OnClickableLabel;

    public RelayCommand<object> OnClickableLabel
    {
        get { return _OnClickableLabel ?? (_OnClickableLabel = new RelayCommand<object>((currentObject) => Test(currentObject))); }
    }

    private void Test(object currentObject)
    {
        Debug.WriteLine("This should work... but it's not working :(");
    }
}

My data model code looks like: 我的数据模型代码如下:

public class MyItem
{
    public long ID { get; set; }
    public string Name { get; set; }

    private RelayCommand<object> _OnClickableLabel;

    public RelayCommand<object> OnClickableLabel
    {
        get { return _OnClickableLabel ?? (_OnClickableLabel = new RelayCommand<object>((currentObject) => Test(currentObject))); }
    }

    private void Test(object currentObject)
    {
        Debug.WriteLine("This works... but it's not good idea, to have it here...");
    }
}

Any idea what needs to be changed, in order to invoke OnClickableLabel directly inside my page ViewModel ? 知道需要改变什么,以便直接在我的页面ViewModel中调用OnClickableLabel I know, that it's something wrong at: 我知道,这是错误的:

<TapGestureRecognizer Command="{Binding OnClickableLabel}" CommandParameter="{Binding .}" />

but don't know what :/. 但不知道:/。

Help! 救命! Thanks a lot. 非常感谢。

Ok, I found solution, by extending XAML code: 好的,我通过扩展XAML代码找到了解决方案:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App.Views.TestView" x:Name="Page">
  <ContentPage.Content>
    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <ListView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" ItemsSource="{Binding MyItemsCollection}" SelectedItem="{Binding SelectedItem}">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout Orientation="Horizontal">

                  <!-- Name Label -->
                  <Label Text="{Binding Name}" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand" />

                  <!-- Delete "Icon" -->
                  <Label Text="Clickable Label" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand">
                    <Label.GestureRecognizers>
                      <TapGestureRecognizer Command="{Binding Path=BindingContext.OnClickableLabel, Source={x:Reference Page}}" CommandParameter="{Binding .}" />
                    </Label.GestureRecognizers>
                  </Label>

                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

After that, I got OnClickableLabel command invoked inside my page ViewModel , as expected :). 之后,我在我的页面ViewModel中调用了OnClickableLabel命令,正如预期的那样:)。

If someone know "better" solution (better from XAML code point of view), I would like to see it ;). 如果有人知道“更好”的解决方案(从XAML代码的角度来看更好),我希望看到它;)。

Thanks a lot everyone! 非常感谢大家!

continuing with what @Namek said i would suggest to get the object of list view item first and then call the command or viewmodel method. 继续@Namek说我建议先获取列表视图项的对象,然后调用命令或viewmodel方法。

for more you can refer my blog post about interactive Listview at https://adityadeshpandeadi.wordpress.com/2018/07/15/the-more-interactive-listview/ 您可以在https://adityadeshpandeadi.wordpress.com/2018/07/15/the-more-interactive-listview/上查阅我关于互动列表视图的博文。

feel free to drop by. 随心所欲。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要修复:无法从Listview-DataTemplate-ViewCell内的Custom控件内的按钮打开模式 - Need Fix: Cannot open modal from button inside a Custom control inside Listview-DataTemplate-ViewCell ViewCell中具有不同边距的元素 - Elements inside ViewCell with diferent margins 在XAML的ListView中嵌套标签 - Nesting a Label inside a ListView in XAML 如何根据Xamarin Forms中的设备使`ViewCell`中的`Label`字体大小? - How to make `Label`'s font size inside `ViewCell` dependent on the device in Xamarin Forms? Xamarin.Forms MVVM TapGestureRecognizer 到 ListView 的 ViewCell 中的标签(在部分文件中) - Xamarin.Forms MVVM TapGestureRecognizer to a Label in a ViewCell of ListView (in partial files) ViewCell中的按钮无法导航到Xamarin Forms中的另一个页面 - Button inside ViewCell is not able to navigate to another page in Xamarin Forms Xamarin.Forms - 更新ItemSource时,ViewCell内的图像闪烁 - Xamarin.Forms - Image inside ViewCell flashes when updating the ItemSource 从视单元内的图像访问来自手势的列表数据 - Access list data from tapgesture from image inside viewcell 如何在 ListView 中为 Xamarin Forms 绑定一个简单的 Label? - How to bind a simple Label inside ListView for Xamarin Forms? 如何在 xamarin 形式的列表视图内的标签中动态设置文本颜色 - How to set textcolor dynamically in label inside listview in xamarin forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM