BindableBase 不适用于 CollectionView

BindableBase not working on CollectionView

我的项目中有一个 CollectionView,其中包含支持 CollectionView (4.3.0.908675) 的最稳定版本的 Xamarin Forms,代码如下。

   <CollectionView x:Name="ScrollButtons"
                ItemsSource="{Binding MenuItems}"
                SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}"
                Grid.Row="2" 
                Grid.Column="0"
                Grid.ColumnSpan="2" 
                HeightRequest="90"
                SelectionMode="Single"
                SelectionChangedCommand="{Binding MenuItemSelectedCommand}"
                BackgroundColor="{DynamicResource BackgroundColorShell}">
    <CollectionView.Footer>
        <!--HACK to keep showing last item on CollectionView -->
        <BoxView BackgroundColor="Transparent" HeightRequest="90" WidthRequest="50"/>
    </CollectionView.Footer>
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Horizontal"
                        Span="1" HorizontalItemSpacing="5"/>
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid WidthRequest="90" HeightRequest="90" Padding="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Frame Grid.Column="0"
                           Grid.Row="0"
                           BorderColor="Black"
                           BackgroundColor="{Binding BackgroundColor}"
                           >
                     </Frame>
                    <StackLayout Padding="5" Grid.Row="0" Grid.Column="0">
                        <Label Text="{Binding Text}"
                               TextColor="{Binding TextColor}"
                               LineBreakMode="WordWrap"
                               FontSize="{StaticResource BaseFontSize}"
                               x:Name="tileLabel">
                        </Label>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand">
                          <Image Source="{Binding SecondaryIconSource}"
                                 HorizontalOptions="Start"
                                 VerticalOptions="EndAndExpand"
                                 WidthRequest="25"
                                 HeightRequest="25"
                                 IsVisible="{Binding IsSecondaryIconVisible}"
                                 />
                          <Image Source= "{Binding ImageIcon}"
                                 HorizontalOptions="EndAndExpand"
                                 VerticalOptions="EndAndExpand"
                                 WidthRequest="25"
                                 HeightRequest="25"
                                 x:Name="tileIcon">
                          </Image>
                        </StackLayout>
                    </StackLayout>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
</CollectionView>

OnMenuSelectedItemCommand

    private async Task OnMenuItemSelected()
    {
        Console.WriteLine("OnMenuItemSelected");
        await NavigationService.NavigateAsync($"{SelectedMenuItem.NavigationPath}");
        HighlightedMenuItem = SelectedMenuItem;
        SelectedMenuItem = null;
    }

CollectionView 由 BottomMenuItem class 的集合组成,它继承自 BindableBase for Prism。我的目标是将 BottomMenuItem 的属性更改为在 CollectionView 上选择的属性。但是,集合视图表现得很奇怪,它仅根据当前不在屏幕中的 BottomMenuItem 进行更改。如下所示,它仅适用于第 5 个项目及以后的项目,即最初未加载到屏幕上的项目。

如有任何帮助,我们将不胜感激。谢谢!

我明白了。问题是我的代码依赖于滚动到某个项目以便突出显示 selectedItem。我假设错误是基于最初加载的项目单元格不起作用但实际上它是不需要滚动的项目。

根据我的 OnMenuItemSelected,我将突出显示的项目传递到下一页。使用下面的 OnNavigatingTo 逻辑处理该逻辑。

    public override async void OnNavigatingTo(INavigationParameters parameters)
    {
        base.OnNavigatingTo(parameters);

        Console.WriteLine("OnNavigatingTo");

        HighlightedMenuItem = parameters.GetValue<BottomMenuItem>("highlightedMenuItem");

        foreach (var item in MenuItems)
        {
            item.IsActive = false;
        }

        if (HighlightedMenuItem != null)
        {
            Console.WriteLine("OnNavigatingTo HighlightedItem - {0}", HighlightedMenuItem.Text);

            HighlightedMenuItem.IsActive = true;
        }
    }