Xamarin.forms ImageButton未在Xamarin表单中触发命令绑定

Xamarin.forms ImageButton未在Xamarin表单中触发命令绑定,xamarin.forms,data-binding,viewmodel,imagebutton,Xamarin.forms,Data Binding,Viewmodel,Imagebutton,我有一个ImageButton,它没有使用VMMV架构触发命令绑定命令。首先,所有其他绑定都在视图中正常工作 以下是按钮: <ImageButton Command="{Binding SelectedItemCommand}" Source="{Binding Logo}" Grid.Row="0" Grid.Column="1" HeightRequest="150" WidthRequ

我有一个ImageButton,它没有使用VMMV架构触发命令绑定命令。首先,所有其他绑定都在视图中正常工作

以下是按钮:

<ImageButton Command="{Binding SelectedItemCommand}" Source="{Binding Logo}" Grid.Row="0" Grid.Column="1" HeightRequest="150" WidthRequest="150" HorizontalOptions="CenterAndExpand" VerticalOptions="EndAndExpand"></ImageButton>
当我点击图像时,什么都没有发生。我甚至尝试绑定到按下的参数,但从我所读到的所有内容来看,命令参数应该在绑定场景中使用。在函数GetSelectedItem上放置断点永远不会到达

我做错了什么

对不起,我离开几天了。所以这些建议没有任何作用,即使它们真的应该单击“不会触发”命令。无论如何,我现在使用一个事件处理程序成功地启动了它,如下所示:

SelectedItemCommand = new Command<string>(param => OnItemSelected(param));

public void OnItemSelected(string img1_2)
{
  PressedEventHandler?.Invoke(this, EventArgs.Empty);
}
但这将返回一个空值,而不是发送方对象值中列出的值

还有其他想法吗

蒂亚! 瑞克


您只能绑定到公共属性-您需要一个
get
才能使其成为PropertyTanks。那么你是说我的公开声明是错误的?我是通过看视频得到这个密码的。我在一个“已解决”的问题中也看到了这一点,这几乎正是我想要做的。无论如何比如说:public ICommand SelectedItemCommand{get;set;}?我如何以这种方式启动我的函数?只需像任何属性一样在构造函数中分配它。你把ImageButton放在哪里了?属性可能会很好。Shaw如果我按照您的问题进行操作,imagebutton位于我的xaml页面中,我的绑定在xaml文件中被引用回我的viewmodel,代码在其中写入。谢谢IInam,但在单击某个项时,两个都不会从命令参数触发?您建议的第一个选项仅在最初加载页面时触发。将视图模型构造函数中的代码更改为SelectedItemCommand=newcommand(GetSelectedItem);我还能够找出如何从发送者对象获取属性。我需要像QItems qitms=(QItems)sender一样再次对我的模型进行转换;然后像这样使用:qitms.Item1Image…等等
SelectedItemCommand = new Command<string>(param => OnItemSelected(param));

public void OnItemSelected(string img1_2)
{
  PressedEventHandler?.Invoke(this, EventArgs.Empty);
}
string str = Item1Image.ToString(); // property in sender and viewmodel
public ICommand SelectedItemCommand {get; private set;}

...

public YourViewModel(){
   ...
   SelectedItemCommand = new Command(GetSelectedItem);
   ...
}
...
public ICommand SelectedItemCommand{
   get
   {
      return new Command(() => {
           //Do something
      });
   }
}