【问题标题】:Xamarin Forms CollectionView Command not workingXamarin Forms CollectionView 命令不起作用
【发布时间】:2020-02-12 07:47:01
【问题描述】:

我有一个绑定了命令的集合视图,但由于某种原因,当我选择一个项目时,该操作从未在视图模型中调用,这是我的 ViewModel 代码:

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));
    }
}

这是我的主页:

<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ComanderoMovil.Views.PlatillosView"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true"
xmlns:behaviorsPack="clr-namespace:Xamarin.Forms.BehaviorsPack;assembly=Xamarin.Forms.BehaviorsPack">
<ContentPage.Content>
    <StackLayout>
        <SearchBar> </SearchBar>
        <StackLayout Orientation="Horizontal">
            <CollectionView ItemsSource="{Binding Grupos}"
                            HeightRequest="50"
                            ItemsLayout="HorizontalList"
                            SelectionMode="Single"
                            SelectedItem="{Binding SelectedGroupCommand, Mode=TwoWay}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <ContentView>
                            <Label Margin="2"
                                    BackgroundColor="Black"
                                    Text="{Binding nombre}"
                                    TextColor="White"
                                   VerticalTextAlignment="Center"
                                   HorizontalTextAlignment="Center"
                                   FontSize="Small"></Label>
                        </ContentView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
        <ListView Grid.Column="2"
                  HasUnevenRows="True"
                  SeparatorVisibility="None"
                  ItemsSource="{Binding Platillos}">
            <ListView.Behaviors>
                <behaviorsPack:SelectedItemBehavior Command="{Binding SelectedPlatilloCommand}"/>
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ContentView Padding="2, 5, 5, 0">
                            <Frame OutlineColor="Black"
                                   Padding="10"
                                   HasShadow="False">
                                <StackLayout Orientation="Horizontal">
                                    <Label Margin="10"
                                           Text="{Binding clave_platillo}"
                                           FontSize="Large"
                                           HorizontalOptions="Start"></Label>
                                    <Label Margin="10"
                                           HorizontalTextAlignment="End"
                                           Text="{Binding nombre}"></Label>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

我尝试将命令添加到集合视图中的项目,替换按钮的标签,但仍然不起作用,我也尝试在集合视图中使用 SelectionChangedCommand,但仍然是同样的问题,唯一我可以让它工作的方法是处理视图中的项目选择,但我想忠于 MVVM。

这是我的 GrupoModel:

public class GrupoModel
{
    public string clave_grupo { get; set; }
    public int id_clasificacion { get; set; }
    public int id_grupo { get; set; }
    public string nombre { get; set; }
    public bool pedirClave { get; set; }
    public bool status { get; set; }
    public int tipo { get; set; }
}

这是我想要做的事情的图像:

【问题讨论】:

  • 该命令应该放在GrupoModel 的类中而不是PlatillosViewModel
  • 什么意思?,我有 PlatillosView 页面和 PlatillosViewModel,没有 GrupoView 或 GrupoViewModel,我想像顶部菜单一样管理集合视图,有点,我会更新我的图像问题,我也尝试将命令放在 grupo 的模型类中,仍然没有工作

标签: xamarin.forms


【解决方案1】:

如果您阅读document

SelectionMode 属性设置为 Single 时, 可以选择CollectionView。当一个项目被选中时, SelectedItem 属性将设置为所选项目的值。 当这个属性改变时,SelectionChangedCommand 被执行 (传递 SelectionChangedCommandParameter 的值 到 ICommand),并且 SelectionChanged 事件触发。

当你想绑定一个 Commond 时,你应该绑定到 SelectionChangedCommand 而不是 SelectedItem。如下更改您的代码,它将起作用:

   <CollectionView 
         HeightRequest="50"
         ItemsLayout="HorizontalList"
         SelectionMode="Single"
         SelectionChangedCommand="{Binding SelectedGroupCommand, Mode=TwoWay}"

    >

【讨论】:

    【解决方案2】:

    该命令应该放在GrupoModel 的类中,而不是PlatillosViewModel

    public List<GrupoModel> Grupos { get; set; }
    

    应该“链接”到具有属性的类GrupoModel 和一个command,它会监听,类似于:

    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);
        }
    }
    

    这样Grupos的每个元素都会有一个命令来监听。

    顺便说一句:Grupos 不应该是 ObservableCollection 吗?

    【讨论】:

    • 使用 ObservableCollection 有什么区别?
    • 另外,我已将 Command 放在 GrupoModel 类中,但它不起作用
    • 一个 ObservableCollection 将监听视图的变化
    • 我只使用 Xamarin.Forms 在 PlatillosView 我有这个代码: InitializeComponent(); this.BindingContext = new PlatillosViewModel(Navigation);
    • 刚刚找到了让它工作的方法,非常感谢你帮助我
    猜你喜欢
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多