我使用MVVM,并在 View 中包含下一个代码:
<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中:
私有(private)RelayCommand setLike;
public ICommand SetLikeCommand
{
get
{
return this.setLike ?? (this.setLike = new RelayCommand(this.SetLike));
}
}
private void SetLike()
{
var t = "fsdf";
}
当我在SetLike()方法中设置断点时,当我点击图像时程序不会停止。也许我做错了什么,绑定(bind)事件在哪里?请帮忙!
最佳答案
您显示的代码从根本上没有错,只是显示的不足以识别您的问题。
以下工作正常:
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";
}
}
关于c# - Windows Phone 8中的图像无法正常工作点击事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17506718/