BindableBase在CollectionView上不起作用

时间:2019-10-29 23:15:54

标签: xamarin xamarin.forms prism

在我的项目上,我的CollectionView具有最稳定的Xamarin Forms版本,该版本支持CollectionView(4.3.0.908675),并带有以下代码。

   <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类的集合组成,该类从BindableBase for Prism继承。我的目标是更改BottomMenuItem的属性,使其成为在CollectionView上选择的属性。但是,集合视图的行为很奇怪,并且仅根据当前不在屏幕中的BottomMenuItem进行更改。如下所示,它仅适用于第5个项目以及以后未在屏幕上加载的项目。

任何帮助将不胜感激。谢谢!

CollectionViewImage

1 个答案:

答案 0 :(得分:1)

我知道了。问题是我的代码依赖于滚动到某个项目才能突出显示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;
        }
    }