【问题标题】:BindableBase not working on CollectionViewBindableBase 在 CollectionView 上不起作用
【发布时间】:2019-10-31 03:41:57
【问题描述】:

我的项目中有一个 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 类的集合组成,该类继承自 Prism 的 BindableBase。我的目标是将 BottomMenuItem 的属性更改为在 CollectionView 上选择的属性。但是,集合视图表现得很奇怪,它仅根据当前不在屏幕中的 BottomMenuItem 进行更改。如下所示,它只对第 5 项及以后的项目起作用,即最初未加载到屏幕上的项目。

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

【问题讨论】:

  • 可以分享MenuItemSelectedCommand的代码吗?
  • @JackHua-MSFT MenuItemSelectedCommand 是一个 ReactiveCommand。我添加了 MenuItemSelectedCommand 调用的方法。使用 Prism Navigation,我为每个菜单项导航到一个导航 url。
  • @JackHua-MSFT 我分离了 SelectedMenuItem 和 HighlightedMenuItem 两个项目之间的绑定,因为 SelectedMenuItem 单独会导致无限循环,因为它基于事件,即使它为空。

标签: xamarin xamarin.forms prism


【解决方案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;
        }
    }

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多