1

我使用 MVVM,并查看下一个代码:

<Image Source="Content/img/heart_gray.png" Width="25" Height="25" Margin="0,0,5,0" HorizontalAlignment="Right" Visibility="{Binding LikeVisability}">
                                                <i:Interaction.Triggers>
                                                    <i:EventTrigger EventName="Tap">
                                                        <cmd:EventToCommand  Command="{Binding SetLikeCommand}" />
                                                    </i:EventTrigger>
                                                </i:Interaction.Triggers>
                                            </Image>

在视图模型中:

私有 RelayCommand setLike;

     public ICommand SetLikeCommand
    {
        get
        {
            return this.setLike ?? (this.setLike = new RelayCommand(this.SetLike));
        }
    }


    private void SetLike()
    {
        var t = "fsdf";
    }

当我在方法 SetLike() 中设置断点时,当我点击图像时程序不会停止。也许我在视图中做错了什么,绑定事件在哪里?请帮忙!

4

1 回答 1

1

您显示的代码没有任何根本性的错误,只是不足以识别您的问题。

以下确实有效:

xml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Image Source="Assets/ApplicationIcon.png" Width="25" Height="25" Margin="0,0,5,0"
           HorizontalAlignment="Right" Visibility="{Binding LikeVisability}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Tap">
                <cmd:EventToCommand  Command="{Binding SetLikeCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Image>
</Grid>

后面的代码:

using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;

public partial class View : PhoneApplicationPage
{
    public View()
    {
        InitializeComponent();

        this.DataContext = new MyViewModel();
    }
}

public class MyViewModel
{
    private ICommand setLike;

    public ICommand SetLikeCommand
    {
        get
        {
            return this.setLike ?? (this.setLike = new RelayCommand(this.SetLike));
        }
    }

    public Visibility LikeVisibility
    {
        get
        {
            return Visibility.Visible;
        }
    }

    private void SetLike()
    {
        var t = "fsdf";
    }
}
于 2013-07-08T09:10:25.847 回答