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

CollectionView preselected not working in xamarin forms

我想在集合视图中有一个预选项目,但它不起作用。正在将数据从视图模型发送到集合视图。我想在集合视图中预选一项。我需要 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>

查看模型

class 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. classObservableCollection自动实现了一个Notify方法,所以不需要私有字段调用PropertyChanged

    public ObservableCollection WorkerFlockDetails { get;放; }

  2. 修改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)));
                 }
             }
         }
     }
    
  3. 让我们转到构造函数。如果 Init() 方法用数据填充你的 ObservableCollection,你应该在选择一个项目之前调用它,否则,集合将为空

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

public HomeWorkerViewModel(){

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

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

}