C# 未调用Xamarin表单图像TapGestureRecognitizer命令
我试图在图像中绑定TapGestureRecognitor(如图所示),但ViewModel中的相对Icommand不会被触发。我正在使用lightMVVM框架 这是我的密码:C# 未调用Xamarin表单图像TapGestureRecognitizer命令,c#,xamarin.forms,C#,Xamarin.forms,我试图在图像中绑定TapGestureRecognitor(如图所示),但ViewModel中的相对Icommand不会被触发。我正在使用lightMVVM框架 这是我的密码: <?xml version="1.0" encoding="utf-8" ?> <CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com
<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ZoccoloManager.Views.DetailsPage"
ItemsSource="{Binding Cows}">
<CarouselPage.ItemTemplate>
<DataTemplate>
<ContentPage x:Name="DetailsPage">
<RelativeLayout>
<Image x:Name="fl_left"
Source="zoccolo_leftside.png"
RelativeLayout.WidthConstraint=
"{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0.3 }"
RelativeLayout.HeightConstraint=
"{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.3 }"
RelativeLayout.XConstraint =
"{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0.07}"
RelativeLayout.YConstraint =
"{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.07}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.TapImageCommand, Source={x:Reference DetailsPage}}" />
</Image.GestureRecognizers>
</Image>
</RelativeLayout>
</ContentPage>
</DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>
在ViewModel中:
public class DetailsViewModel : ViewModelBase
{
private readonly INavigationService _navigationService;
private IRepository _repository;
public ICommand TapImageCommand { get; private set; }
public ObservableCollection<Cow> Cows { get; set; }
public DetailsViewModel(INavigationService navigationService)
{
_repository = ServiceLocator.Current.GetInstance<IRepository>();
Debug.WriteLine(DateTime.Now + ": Calling LoadCows");
LoadCows();
Debug.WriteLine(DateTime.Now + ": Called LoadCows");
_navigationService = navigationService;
TapImageCommand = new Command(OpenPopup);
}
private async Task LoadCows()
{
Debug.WriteLine(DateTime.Now + ": Started execution LoadCows");
await Task.Run(() =>
{
Cows = new ObservableCollection<Cow>();
foreach (var cow in _repository.GetCompany(0).Cows)
{
Cows.Add(cow);
}
});
Debug.WriteLine(DateTime.Now + ": Finished execution LoadCows");
}
private void OpenPopup()
{
Debug.WriteLine("Opening popup ");
}
}
public类详细信息viewmodel:ViewModelBase
{
私有只读INavigationService(导航服务);
私人IRepository_存储库;
公共ICommand TapImageCommand{get;private set;}
公共可观测集合{get;set;}
公共详细信息查看模型(INavigationService导航服务)
{
_repository=ServiceLocator.Current.GetInstance();
Debug.WriteLine(DateTime.Now+“:调用LoadCows”);
LoadCows();
Debug.WriteLine(DateTime.Now+“:调用LoadCows”);
_导航服务=导航服务;
TapImageCommand=新命令(OpenPopup);
}
专用异步任务LoadCows()
{
Debug.WriteLine(DateTime.Now+“:已开始执行加载”);
等待任务。运行(()=>
{
Cows=新的可观察集合();
foreach(var cow在_repository.GetCompany(0.Cows)中)
{
奶牛。添加(奶牛);
}
});
Debug.WriteLine(DateTime.Now+“:Finished execution LoadCows”);
}
私有void OpenPopup()
{
Debug.WriteLine(“打开弹出窗口”);
}
}
一切加载都很好,但调试它时,永远不会调用ICommand TapImageCommand的getter。对ObservableCollection的绑定工作正常,我有正确的页数作为列表中的元素
我遗漏了什么???好的,我已经找出了问题所在。
Source={x:Reference DetailsPage}
与xml命名空间定义上的类名冲突。
在主标记转盘页面中添加适当的
x:Name
,并在引用中使用相同的名称,使其工作正常。检查您的绑定表达式您能解释一下吗?我还尝试使用TapGestureRecognitzer的命令来“{Binding TapImageCommand}”CommandParameter=“1”,但结果与绑定命令相同,尝试只设置点击事件句柄是的,这会起作用,但据我所知,它会在代码中生成一个方法。我正在尝试直接调用viewmodel。。。