我使用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>
在viewModel中:
私人RelayCommand setLike;
public ICommand SetLikeCommand
{
get
{
return this.setLike ?? (this.setLike = new RelayCommand(this.SetLike));
}
}
private void SetLike()
{
var t = "fsdf";
}
当我在方法SetLike()中设置断点时,程序在我点击图像时不会停止。也许我在视图中做错了,绑定事件在哪里?请帮忙!
答案 0 :(得分:1)
您所展示的代码没有任何根本性的错误,它只是不足以识别您的问题。
以下工作正常:
XAML:
<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";
}
}