Xamarin.forms Xamarin Forms CollectionView命令不工作
我有一个绑定了命令的集合视图,但由于某些原因,当我选择一个项目时,该操作从未在viewmodel中调用,下面是我的viewmodel代码:Xamarin.forms Xamarin Forms CollectionView命令不工作,xamarin.forms,Xamarin.forms,我有一个绑定了命令的集合视图,但由于某些原因,当我选择一个项目时,该操作从未在viewmodel中调用,下面是我的viewmodel代码: public class PlatillosViewModel : INotifyPropertyChanged { private INavigation Navigation; public event PropertyChangedEventHandler PropertyChanged; public List<Plat
public class PlatillosViewModel : INotifyPropertyChanged
{
private INavigation Navigation;
public event PropertyChangedEventHandler PropertyChanged;
public List<PlatilloModel> Platillos { get; set; }
public List<GrupoModel> Grupos { get; set; }
public ICommand SelectedGroupCommand => new Command(SelectedGroup);
public PlatillosViewModel(INavigation navigation)
{
Navigation = navigation;
PlatillosRepository repository = new PlatillosRepository();
Platillos = repository.GetAll().ToList();
GrupoRepository grupoRepository = new GrupoRepository();
Grupos = grupoRepository.GetAll().ToList();
}
public ICommand SelectedPlatilloCommand => new Command<PlatilloModel>(async platillo =>
{
await Navigation.PushAsync(new PlatilloView());
});
void SelectedGroup()
{
PlatillosRepository platillosRepository = new PlatillosRepository();
//Platillos = platillosRepository.GetFilteredByGroup(grupoSeleccionado);
}
protected virtual void OnPropertyChanged(string property = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
下面是我想做的事情的图像:
该命令应位于
GrupoModel
类中,而不是PlatillosViewModel
public List<GrupoModel> Grupos { get; set; }
这样,Grupos
的每个元素都有一个监听命令
顺便说一句:如果您阅读以下内容,Grupos不应该是一个可观察的集合吗
当SelectionMode
属性设置为Single时,将在
可以选择收集视图。选择某个项目后,将显示
SelectedItem
属性将设置为所选项目的值。
当此属性更改时,将执行选择更改命令
(传递selectionChangedCommand参数的值时
,并触发SelectionChanged事件
当您想绑定公共命令时,应该绑定到SelectionChangedCommand
而不是SelectedItem
。按以下方式更改代码,它将正常工作:
<CollectionView
HeightRequest="50"
ItemsLayout="HorizontalList"
SelectionMode="Single"
SelectionChangedCommand="{Binding SelectedGroupCommand, Mode=TwoWay}"
>
命令应该放在GrupoModel
类中,而不是PlatillosViewModel
你的意思是什么?我有PlatillosView页面,PlatillosViewModel,没有GrupoView或GrupoViewModel,我想像顶部菜单一样管理集合视图,我会用图片更新我的问题,我还试着把命令放在grupo的model类中,但仍然不起作用。使用ObservableCollection有什么区别?另外,我把命令放在GrupoModel类中,它不起作用一个ObservableCollection只使用Xamarin.Forms在PlatillosView中监听viewIm之间的更改我有以下代码:InitializeComponent();this.BindingContext=新平台视图模型(导航);刚刚找到了让它工作的方法,非常感谢你在任何方面对我的帮助
public List<GrupoModel> Grupos { get; set; }
Class GrupoModel
{
public int Id { get; set; }
public string Foo { get; set; }
public ICommand SelectedGroupCommand => new Command(Completar);
private async void Completar()
{
await ViewModels.PlatillosViewModel.GetInstancia().SelectedGroup(this);
}
}
<CollectionView
HeightRequest="50"
ItemsLayout="HorizontalList"
SelectionMode="Single"
SelectionChangedCommand="{Binding SelectedGroupCommand, Mode=TwoWay}"
>