我目前正在.NET 8中的Maui项目上工作,但我在CollectionView中使用的TapGestureRecognizer遇到了麻烦。
我的XAML代码:
<ContentPage.BindingContext>
<viewmodel:SearchLoadingListVM />
</ContentPage.BindingContext>
<Page.Behaviors>
<ctk:StatusBarBehavior StatusBarColor="#005ea8" StatusBarStyle="LightContent" />
</Page.Behaviors>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="60*" />
</Grid.RowDefinitions>
<VerticalStackLayout Grid.Row="0"
Grid.RowSpan="2"
Margin="0,30,0,0">
<Image Source="deliveryboximage.png"
HeightRequest="150" />
<HorizontalStackLayout HorizontalOptions="Center">
<Entry BackgroundColor="White"
HeightRequest="40"
WidthRequest="280"
MaxLength="10"
TextTransform="Uppercase"
Placeholder="Auftragsnummer"
HorizontalTextAlignment="Start"
Margin="0,0,-80,0"
IsEnabled="{Binding IsEntryEnabled}"
Text="{Binding EntryTextValue}"
TextColor="Black" />
<ImageButton Source="barcode_scanner.svg"
Scale="0.7"
Margin="0,0,0,7"
Command="{Binding ScanBarCodeCommand}" />
<ImageButton Source="search.svg"
Scale="0.7"
Margin="-5,0,0,7"
Command="{Binding ExecuteSearchCommand}" />
</HorizontalStackLayout>
</VerticalStackLayout>
<ScrollView IsEnabled="{Binding IsScrollViewEnabled}"
Grid.Row="1">
<CollectionView
ItemsSource="{Binding LoadingListCollection}"
HorizontalOptions="Center"
SelectionMode="None">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type x:String}">
<Grid Padding="2">
<Frame BorderColor="LightGray">
<Frame.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SearchLoadingListVM}}, Path=ItemTapedCommand}"
CommandParameter="{Binding .}" />
</Frame.GestureRecognizers>
<HorizontalStackLayout>
<Label Text="{Binding .}"
FontSize="22"
TextColor="Black" />
</HorizontalStackLayout>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
</Grid>
字符串
MVVM Community Toolkit生成的代码:
partial class SearchLoadingListVM
{
/// <summary>The backing field for <see cref="ItemTapedCommand"/>.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
private global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand<string>? itemTapedCommand;
/// <summary>Gets an <see cref="global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand{T}"/> instance wrapping <see cref="ItemTaped"/>.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand<string> ItemTapedCommand => itemTapedCommand ??= new global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand<string>(new global::System.Func<string, global::System.Threading.Tasks.Task>(ItemTaped));
}
型
VM(ItemTaped()应该被调用):
public partial class SearchLoadingListVM : ObservableObject
{
[ObservableProperty]
private ObservableCollection<string> loadingListCollection;
[ObservableProperty]
private string entryTextValue;
[ObservableProperty]
private bool isEntryEnabled = true;
[ObservableProperty]
private bool isScrollViewEnabled;
private WebService.WebService webService;
public SearchLoadingListVM()
{
CheckPermission();
loadingListCollection = new ObservableCollection<string>();
webService = new WebService.WebService();
}
[RelayCommand]
public async void ScanBarCode()
{
IsEntryEnabled = false;
await Shell.Current.GoToAsync( nameof( CodeScannerView ) );
IsEntryEnabled = true;
}
[RelayCommand]
public async void ExecuteSearch()
{
if( !string.IsNullOrEmpty( EntryTextValue ) )
{
IsEntryEnabled = false;
await Shell.Current.Navigation.PushModalAsync( new BusyIndicationModal() );
LoadingListCollection = await webService.GetLoadingListsAsync( EntryTextValue );
if( LoadingListCollection.Count == 0 )
{
}
await Shell.Current.Navigation.PopModalAsync();
IsEntryEnabled = true;
}
}
[RelayCommand]
async Task ItemTaped( string item )
{
await Shell.Current.Navigation.PushAsync( new CameraView() );
}
async Task CheckPermission()
{
PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Camera>();
if( status != PermissionStatus.Granted )
{
status = await Permissions.RequestAsync<Permissions.Camera>();
}
}
型
我确实从James Montemagno的视频中复制了XAML代码和this问题的答案。
有趣的是,它以前确实工作过,但在最后一次Visual Studio更新之后,我的整个解决方案崩溃了,甚至在恢复了一些以前使用的代码之后,它也没有工作。
当我在上面看到的两个方法上设置断点时,工作室不会在任何一个方法上中断。还有一些其他的RelayCommands实现,它们都工作正常,所以我猜问题是我的XAML?
有谁知道我是否遗漏了什么或者为什么它不起作用?
在此先谢谢您!
1条答案
按热度按时间a64a0gku1#
我可以复制这个问题。
这是因为你设置了
<ScrollView IsEnabled="{Binding IsScrollViewEnabled}"
。IsScrollViewEnabled
的默认值是false。你禁用了父控件(ScrollView),子控件也被禁用了。你也可能会发现文本是灰色的。如果你设置了
IsScrollViewEnabled=true
。那么标签textcolor将是黑色的,命令将被触发。如果您有任何问题,请告诉我。