我有一个绑定到记录列表的集合视图。 我在上面的列表下方嵌套了网格,以显示用于导航到记录的不同页面的分页控件。
单击图像按钮时不会触发嵌套网格内的分页控件。
我在嵌套网格之外也有命令,它们会触发视图模型事件。
我正在使用
Command="{Binding BindingContext.FirstCommand, Source={x:Reference ParentGrid}}"
其中parentGrid是父网格的名称。
我在这里缺少什么?
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Matri.Views.AllProfilesPage"
xmlns:FoA="clr-namespace:Matri.FontsAwesome;assembly=Matri"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Title="All Profiles" BackgroundColor="White">
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0">
<ActivityIndicator IsRunning="True" IsVisible="{Binding IsBusy}"></ActivityIndicator>
</StackLayout>
<StackLayout Grid.Row="1" IsVisible="{Binding ShowRecordsNotFoundSection}">
<Label FontSize="Medium" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Text="Records not found."></Label>
</StackLayout>
<StackLayout Grid.Row="2" IsVisible="{Binding ShowRecordsSection}">
<CollectionView x:Name="profilesCollectionView" ItemsSource="{Binding Profiles}"
VerticalOptions="FillAndExpand" SelectionMode="Single"
SelectionChangedCommand="{Binding ViewProfileCommand}"
SelectedItem="{Binding SelectedProfile}"
SelectionChangedCommandParameter="{Binding Path=SelectedItem, Source={x:Reference profilesCollectionView}}"
>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="1"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="94"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Frame HorizontalOptions="Center" IsClippedToBounds="True" CornerRadius="4" HasShadow="False" Grid.Row="0"
Grid.Column="0" HeightRequest="72" WidthRequest="72" Padding="0">
<Image Grid.Column="0" Grid.Row="0" HorizontalOptions="Center" VerticalOptions="FillAndExpand"
HeightRequest="72" WidthRequest="72"
Aspect="Fill">
<Image.Source>
<UriImageSource Uri="{Binding ThumbNailUri}"/>
</Image.Source>
</Image>
</Frame>
<Grid Grid.Row="0" Grid.Column="1" VerticalOptions="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding FirstName}" LineBreakMode="WordWrap"
TextColor="#DE000000" FontFamily="Roboto-Medium"
FontSize="{OnPlatform Android={OnIdiom Phone=15, Tablet=22}, iOS=16, WinUI=14,MacCatalyst=16}"
CharacterSpacing="0.1" Margin="0,10,0,0"/>
<!--<Label Grid.Row="0" Grid.Column="1" Text="1w ago" TextColor="#666666"
FontSize="{OnPlatform Default=10,WinUI=12,MacCatalyst=12}" FontFamily="Roboto-Regular"
HorizontalOptions="End" CharacterSpacing="0.15" Margin="0,10,11,0"/>-->
<Label Grid.Row="1" Grid.Column="0" LineBreakMode="WordWrap" TextColor="#666666"
LineHeight="{OnPlatform iOS={OnIdiom Tablet=1.2, Default=1.025}, Default=1.2}"
Text="{Binding Height}" FontFamily="Roboto-Regular" FontSize="14" CharacterSpacing="0.1"
Margin="0,4,0,0" />
<Label Grid.Row="2" Grid.Column="0" LineBreakMode="WordWrap" TextColor="#666666"
LineHeight="{OnPlatform iOS={OnIdiom Tablet=1.2, Default=1.025}, Default=1.2}"
Text="{Binding Age}" FontFamily="Roboto-Regular" FontSize="14" CharacterSpacing="0.1"
Margin="0,4,0,0" />
<Label Grid.Row="3" Grid.Column="0" LineBreakMode="WordWrap" TextColor="#666666"
LineHeight="{OnPlatform iOS={OnIdiom Tablet=1.2, Default=1.025}, Default=1.2}"
Text="{Binding Education}" FontFamily="Roboto-Regular" FontSize="14" CharacterSpacing="0.1"
Margin="0,4,0,0" />
<Label Grid.Row="4" Grid.Column="0" LineBreakMode="WordWrap" TextColor="#666666"
LineHeight="{OnPlatform iOS={OnIdiom Tablet=1.2, Default=1.025}, Default=1.2}"
Text="{Binding Job}" FontFamily="Roboto-Regular" FontSize="14" CharacterSpacing="0.1"
Margin="0,4,0,10" />
</Grid>
<BoxView Margin="0,0,0,0" Grid.Row="1" Grid.Column="1" BackgroundColor="Black" HeightRequest="1"
VerticalOptions="End" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<!--<Frame Margin="0,5,0,0" BackgroundColor="AliceBlue" Padding="10">-->
<Grid x:Name="ParentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="0,18,0,15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ImageButton Aspect="AspectFill" Grid.Row="0" Grid.Column="0"
HeightRequest="25" VerticalOptions="CenterAndExpand" x:Name="First"
Command="{Binding BindingContext.FirstCommand, Source={x:Reference ParentGrid}}"
HorizontalOptions="Start" WidthRequest="25" >
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesomeSolid"
Glyph="{x:Static FoA:FontAwesomeIcons.AngleDoubleLeft}" Color="Gray" />
</ImageButton.Source>
</ImageButton>
<ImageButton HorizontalOptions="Start" Aspect="AspectFill"
HeightRequest="25" WidthRequest="25" VerticalOptions="Center" Grid.Row="0" Grid.Column="1"
Command="{Binding PreviousCommand}">
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesomeSolid"
Glyph="{x:Static FoA:FontAwesomeIcons.AngleLeft}" Color="Gray" />
</ImageButton.Source>
</ImageButton>
<ImageButton HorizontalOptions="End" Aspect="AspectFill" Grid.Row="0" Grid.Column="2"
HeightRequest="25" WidthRequest="25" VerticalOptions="Center" Command="{Binding NextCommand}">
<ImageButton.Source>
<FontImageSource
FontFamily="FontAwesomeSolid"
Glyph="{x:Static FoA:FontAwesomeIcons.AngleRight}"
Color="Gray" />
</ImageButton.Source>
</ImageButton>
<ImageButton HorizontalOptions="End" Aspect="AspectFill" Grid.Row="0" Grid.Column="3"
HeightRequest="25" WidthRequest="25" VerticalOptions="CenterAndExpand" x:Name="Last"
Command="{Binding LastCommand}">
<ImageButton.Source>
<FontImageSource
FontFamily="FontAwesomeSolid"
Glyph="{x:Static FoA:FontAwesomeIcons.AngleDoubleRight}"
Color="gray" />
</ImageButton.Source>
</ImageButton>
</Grid>
<Label Grid.Row="1" FontSize="Small" Grid.Column="0" Text="Profiles Per Page"
VerticalOptions="Center" HorizontalOptions="Start"></Label>
<Picker x:Name="PickerPageSize" Grid.Row="1" Grid.Column="2"
VerticalOptions="Center" ItemsSource="{Binding PageSizeList}" HorizontalOptions="Start"
ItemDisplayBinding="{Binding Text}" SelectedItem="{Binding PPageSize}" >
<Picker.Behaviors>
<toolkit:EventToCommandBehavior
EventName="SelectedIndexChanged"
Command="{Binding SelectedPageSizeCommand}"
CommandParameter="{ Binding .}" />
</Picker.Behaviors>
</Picker>
<Label Grid.Row="2" FontSize="Small" Grid.Column="0" Text="Current Page"
VerticalOptions="Center"/>
<Entry Keyboard="Numeric" Text="{Binding EPageNumber}" HorizontalOptions="Start" Grid.Row="2"
Grid.Column="2" FontSize="Small" VerticalOptions="End" Margin="0,0,0,0"/>
<Label Grid.Row="2" Grid.Column="3" FontSize="Small" VerticalOptions="Center" HorizontalOptions="End"
Text="{Binding LNumberOfPages}" />
</Grid>
<!--</Frame>-->
</StackLayout>
</Grid>
</ScrollView>
</ContentPage>
using Matri.ViewModel;
namespace Matri.Views;
public partial class AllProfilesPage : ContentPage
{
public AllProfilesPage(AllProfilesViewModel viewModel)
{
InitializeComponent();
BindingContext = viewModel;
}
}
[RelayCommand]
private async Task Next()
{
int ePN = Convert.ToInt32(EPageNumber);
if (ePN < totalPages)
{
ePN = ePN + 1;
await GetProfiles(ePN, Convert.ToInt16(PPageSize.Value));
EPageNumber = ePN.ToString();
}
}
我认为问题可能出在造型上。你有相当复杂的 UI 和嵌套的 Grid。最重要的是,您正在使用ImageButton。尝试将 BackgroundColor 添加到图像按钮。这将显示图像按钮在视图中的实际位置。图像按钮的图像有可能在视图中排列不正确。
您也可以检查设置ImageButton的Source是否正常,问题是否仍然存在...Source =“image.png”