【问题标题】:Xamarin.Forms Image with TapGestureRecognizer带有 TapGestureRecognizer 的 Xamarin.Forms 图像
【发布时间】:2019-11-16 17:41:23
【问题描述】:

我是 Xamarin.Forms 的初学者。请帮忙:

我有一个列表视图:

 <ListView Grid.Row="2" ItemsSource="{Binding PostsVm.Posts}" HasUnevenRows="True" 
                      SelectedItem="{Binding PostsVm.SelectedPost, Mode=TwoWay}"
                      ItemSelected="DisableSelection">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="50" />
                                </Grid.RowDefinitions>
                                <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Grid.Row="2" Margin="10,0,10,0">
                                    <Image Source="{Binding Like}" WidthRequest="30">
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer
                                               Tapped="AddLike"
                                               NumberOfTapsRequired="1" />
                                        </Image.GestureRecognizers>
                                   </Image>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

点击后我需要更改 Image.Source。但在 CodeBehind 中,我需要 ListView 选定对象。

后视图模型:

private bool _isLiked;

public bool IsLiked
{
    get { return _isLiked; }
    set
    {
        SetValue(ref _isLiked, value);
        OnPropertyChanged(Like);
    }
}

public string Like
{
    get { return IsLiked ? "likered.png" : "like.png"; }
}

PostsViewModel:

public ObservableCollection<PostViewModel> Posts { get; private set; }
public ICommand AddLikeCommand { get; private set; }
private PostViewModel _selectedPost;
public PostViewModel SelectedPost
{
    get { return _selectedPost; }
    set { SetValue(ref _selectedPost, value); }
}
public PostsViewModel()
{
    Posts = new ObservableCollection<PostViewModel>();
    Image = "http://lorempixel.com/output/sports-q-c-640-480-9.jpg",
    });
    AddLikeCommand = new Command<PostViewModel>(vm => AddLike(vm));
}

private void SelectPost(PostViewModel post)
{
    if (post == null)
        return;
    SelectedPost = null;
}

private void AddLike(PostViewModel post)
{
    if (post == null)
        return;
    post.IsLiked = true;
    SelectedPost = null;
}

如何从 XAML 将 PostViewModel 发布到我的方法? 当用户像我一样点击时,我需要更改该图像。但在代码中,我需要 listView Item 对象来更改它的属性。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    发件人的BindingContext 应该包含您需要的数据

    private void AddLike(object sender, EventArgs args)
    {
        var post = (PostViewModel) ((Image)sender).BindingContext;
        if (post == null)
            return;
        post.IsLiked = true;
        SelectedPost = null;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多