预选的 CollectionView 在 xamarin 表单中不起作用

Posted

技术标签:

【中文标题】预选的 CollectionView 在 xamarin 表单中不起作用【英文标题】:CollectionView preselected not working in xamarin forms 【发布时间】:2021-12-01 19:30:13 【问题描述】:

我想在集合视图中有一个预先选择的项目,但它不起作用。正在将数据从视图模型发送到集合视图。我想在集合视图中预选一个项目。我在 xamarin 需要帮助。这是我的代码

Xaml

<CollectionView x:Name="WorkerFlockView" ItemsSource="Binding WorkerFlockDetails" SelectedItem="Binding SelectedFlock" SelectionMode="Single" SelectionChangedCommand="Binding ViewFlockControlPointsCommand" HeightRequest="100">
                    <CollectionView.ItemsLayout>
                        <LinearItemsLayout Orientation="Horizontal" ItemSpacing="10" />
                    </CollectionView.ItemsLayout>
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Frame Margin="0,0,0,0" Padding="0" HorizontalOptions="CenterAndExpand" BackgroundColor="#F7F7F7" Opacity="0.93" HasShadow="OnPlatform android=true, ios=false"  CornerRadius="0" IsClippedToBounds="True" >
                                    <StackLayout Orientation="Vertical" Opacity="1">
                                        <StackLayout HeightRequest="15" BackgroundColor="#105CA5"></StackLayout>
                                        <StackLayout Orientation="Vertical" Padding="10,0,10,0" VerticalOptions="CenterAndExpand">
                                            <StackLayout Orientation="Horizontal">
                                                <Label Text="Flock" TextColor="#105CA5"></Label>
                                                <Label Text="Binding Flock_name"></Label>
                                            </StackLayout>
                                            <StackLayout Orientation="Horizontal">
                                                <Label Text="Farm" TextColor="#105CA5"></Label>
                                                <Label Text="Binding Farm_name"></Label>
                                            </StackLayout>
                                            <StackLayout Orientation="Horizontal">
                                                <Label Text="House" TextColor="#105CA5"></Label>
                                                <Label Text="Binding House_name"></Label>
                                            </StackLayout>
                                            <StackLayout Orientation="Horizontal">
                                                <Label Text="Age" TextColor="#105CA5"></Label>
                                                <Label Text="Binding Flock_age"></Label>
                                            </StackLayout>
                                            <StackLayout Orientation="Horizontal">
                                                <Label Text="Bird Type" TextColor="#105CA5"></Label>
                                                <Label Text="Binding Bird_type"></Label>
                                            </StackLayout>
                                            <StackLayout Orientation="Horizontal">
                                                <Label Text="Number of birds" TextColor="#105CA5"></Label>
                                                <Label Text="Binding Number_of_birds"></Label>
                                            </StackLayout>

                                        </StackLayout>
                                    </StackLayout>
                                </Frame>

                                <VisualStateManager.VisualStateGroups >
                                    <VisualStateGroup Name="CommonStates">
                                        <VisualState Name="Normal" />
                                        <VisualState Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="BackgroundColor" Value="Blue" />

                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

查看模型

类 HomeWorkerViewModel : INotifyPropertyChanged

public HomeWorkerViewModel()

   WorkerFlockDetails = new ObservableCollection<WorkerFlock>();

   selectedFlock = WorkerFlockDetails.Skip(3).FirstOrDefault();

  //function for adding information
  Init();


//Populate WorkerFlockDetails with data
function Init()


public ObservableCollection<WorkerFlock> workerFlockDetails;
    public ObservableCollection<WorkerFlock> WorkerFlockDetails
    
        get => workerFlockDetails;
        set
        
            workerFlockDetails = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(WorkerFlockDetails)));
        
    

private WorkerFlock selectedFlock  get; set; 
    public WorkerFlock SelectedFlock
    
        get  return selectedFlock; 
        set
        
            if (selectedFlock != value)
            
                try
                
                    //Feed = selectedFlock.Feed_intake.ToString() + " Kg";
                    selectedFlock = value;                       

                
                catch
                

                
            
        
    



【问题讨论】:

【参考方案1】:

有几点:

    ObservableCollection 类自动实现了一个 Notify 方法,所以你不需要私有字段并调用PropertyChanged

    public ObservableCollection WorkerFlockDetails get;放;

    修改SelectedFlock时应该调用PropertyChanged

    private WorkerFlock selectedFlock;
    public WorkerFlock SelectedFlock
    
         get => selectedFlock
         set
         
             if (selectedFlock != value)
             
                 try
                 
                     //Feed = selectedFlock.Feed_intake.ToString() + " Kg";
                     selectedFlock = value;    
                 
                 catch
                 
    
                 
                 finally 
                 
                  PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedFlock)));
                 
             
         
     
    

    让我们转到构造函数。如果Init() 方法用数据填充你的 ObservableCollection,你应该在选择一个项目之前调用它,否则,集合将为空

并且在使用代码更改选择时,您应该调用公共属性而不是私有属性,因为它会通知 UI 它已更改

public HomeWorkerViewModel()

   WorkerFlockDetails = new ObservableCollection<WorkerFlock>();
    
   //Call init before selection
   //function for adding information. 
   Init();

   //Use public property
   SelectedFlock = WorkerFlockDetails.Skip(3).FirstOrDefault();


【讨论】:

以上是关于预选的 CollectionView 在 xamarin 表单中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Xama不会跟踪Xamarin Android新文件

升级到 Visual Studio 16.11.1 后,Xamarin Forms 破坏了 XANS 7028:System.IO.FileNotFoundException:无法加载程序集“Xama

如何在下拉列表框中预选? [复制]

ImageAreaSelec:它不会在初始化时预览预选

在 select2 中预选值

ExtJS 4.1:如何在组合框中设置预选项目?