XAML 无法通过组件名称访问代码后面的组件

b1zrtrql  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(92)

我试图访问集合视图的名称在代码后面是另一个集合视图的子视图,但子集合视图的在代码后面无法识别。
这是我的xaml代码,我试图在另一个集合视图中访问一个集合视图。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="Internal_SME_UI.Views.FlyoutMenuPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:helpers="clr-namespace:Internal_SME_UI.Helpers"
    xmlns:models="clr-namespace:SME_Data.Models;assembly=SME_Data"
    xmlns:vm="clr-namespace:Internal_SME_UI.ViewModels"
    Title="FlyoutMenuPage"
    x:DataType="vm:FlyoutMenuViewModel">

    <StackLayout
        Margin="10,20"
        HorizontalOptions="FillAndExpand"
        Spacing="10"
        VerticalOptions="FillAndExpand">

        <CollectionView
            x:Name="collectionViewFlyout"
            Margin="3,15,5,15"
            x:FieldModifier="public"
            ItemsSource="{Binding FlyoutItems}"
            SelectionChanged="collectionViewFlyout_SelectionChanged"
            SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate x:Name="subMenuItems" x:DataType="models:FlyoutItemModel">

                    <Grid RowDefinitions="Auto,auto">
                        <Grid Grid.Row="0" Padding="5,10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="30" />
                            </Grid.ColumnDefinitions>

                            <Label
                                x:Name="iconLbl"
                                FontFamily="Icon"
                                FontSize="16"
                                HorizontalOptions="Center"
                                Text="{Binding MenuIcon}"
                                TextColor="#00416d" />
                            <Label
                                x:Name="titleLbl"
                                Grid.Column="1"
                                Margin="10,0"
                                FontAttributes="Bold"
                                FontSize="16"
                                Text="{Binding Title}"
                                TextColor="#00416d" />

                        </Grid>
                        <CollectionView
                            x:Name="subMenuItemsCollectionView"
                            Grid.Row="1"
                            x:FieldModifier="public"
                            ItemsSource="{Binding SubItems}"
                            SelectionChanged="subMenuItemsCollectionView_SelectionChanged"
                            SelectionMode="Single">
                            <CollectionView.ItemTemplate>
                                <DataTemplate x:Name="subMenuItems" x:DataType="models:FlyoutItemModel">

                                    <Grid Margin="10,0,0,0" Padding="5,10">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="30" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <Label
                                            x:Name="iconLblSubMenu"
                                            FontFamily="Icon"
                                            FontSize="16"
                                            HorizontalOptions="Center"
                                            Text="{Binding MenuIcon}"
                                            TextColor="#00416d" />
                                        <Label
                                            x:Name="titleLblSubMenu"
                                            Grid.Column="1"
                                            Margin="10,0"
                                            FontAttributes="Bold"
                                            FontSize="16"
                                            Text="{Binding Title}"
                                            TextColor="#00416d" />

                                        <VisualStateManager.VisualStateGroups>
                                            <VisualStateGroup Name="CommonStates">
                                                <VisualState Name="Normal" />
                                                <VisualState Name="Selected">
                                                    <VisualState.Setters>
                                                        <Setter Property="BackgroundColor" Value="#00416d" />
                                                        <Setter TargetName="titleLblSubMenu" Property="Label.TextColor" Value="White" />
                                                        <Setter TargetName="iconLblSubMenu" Property="Label.TextColor" Value="White" />
                                                    </VisualState.Setters>
                                                </VisualState>
                                            </VisualStateGroup>
                                        </VisualStateManager.VisualStateGroups>
                                    </Grid>

                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="CommonStates">
                                <VisualState Name="Normal" />
                                <VisualState Name="Selected">
                                    <VisualState.Setters>
                                        <Setter Property="BackgroundColor" Value="#00416d" />
                                        <Setter TargetName="titleLbl" Property="Label.TextColor" Value="White" />
                                        <Setter TargetName="iconLbl" Property="Label.TextColor" Value="White" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>

                </DataTemplate>

            </CollectionView.ItemTemplate>
        </CollectionView>

    </StackLayout>
</ContentPage>

字符串
这是我后面的代码,其中collectionViewFlyout被识别,但名称为subMenuItemsCollectionView的集合视图无法识别,并在此代码的最后一行给出错误。

using Internal_SME_UI.ViewModels;
using SME_Data.Models;

namespace Internal_SME_UI.Views;

public partial class FlyoutMenuPage : ContentPage
{
    private readonly FlyoutMenuViewModel _flyoutMenuViewModel;
    public FlyoutMenuPage(FlyoutMenuViewModel flyoutMenuViewModel)
    {
        InitializeComponent();
        _flyoutMenuViewModel = flyoutMenuViewModel;
        BindingContext = flyoutMenuViewModel;
        collectionViewFlyout.SelectedItem = flyoutMenuViewModel.FlyoutItems[1];
        

    }
    public FlyoutMenuPage():this(new FlyoutMenuViewModel()) { }

    private void collectionViewFlyout_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        
        if (e.CurrentSelection.FirstOrDefault() is FlyoutItemModel selectedItem)
        {
            if (selectedItem == _flyoutMenuViewModel.FlyoutItems[5])
            {
                collectionViewFlyout.SelectedItem = e.PreviousSelection;    
            }
        }
    }

    private void subMenuItemsCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        collectionViewFlyout.SelectedItem = null;
        subMenuItemsCollectionView.SelectedItem = e.CurrentSelection;
    }
}

zzzyeukh

zzzyeukh1#

我们通常不建议在CollectionView内部使用CollectionView,这可能会导致各种问题。
您可以将内部的CollectionView替换为BindableLayout
我们通常不使用x:Name来访问CollectionView中的控件,你可以使用数据绑定来代替。
你可以参考thread:Add Button in DataTemplate in CollectionView .netMaui

相关问题