.NET MAUI EventToCommandBehavior -由于对象的当前状态,操作无效

bihw5rsg  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(158)

我在页面上绑定了一个CollectionView,其中包含每个记录的ImageButton控件。我正在尝试为Clicked事件添加一个处理程序,该事件将参数传递给Command。由于CollectionView有自己的DataTemplate/DataType,我相信我必须在命令的绑定上使用RelativeSource选项。
一旦CollectionView加载,我就会在应用程序级别获得一个讨厌的未处理异常(由于对象的当前状态,操作无效)。
x1c 0d1x的数据
我试过所有的模式。
我已经尝试了路径只是PdfViewCommand,BindingContext.PdfViewCommand和它下面。
我也尝试过不带参数的情况下,行为没有任何变化。
请注意,在RelayCommand被命中之前,异常就会弹出。

  1. <CollectionView.ItemTemplate>
  2. <DataTemplate x:DataType="model:PatientEd">
  3. <VerticalStackLayout BackgroundColor="Transparent" Margin="2" >
  4. <ImageButton Source="{Binding PatientEdThumbUri}" HeightRequest="85" Margin="2" CornerRadius="10" HorizontalOptions="Center" BorderColor="White" BorderWidth="2" ToolTipProperties.Text="{Binding PatientEdName}">
  5. <ImageButton.Behaviors>
  6. <toolkit:EventToCommandBehavior
  7. EventName="Clicked"
  8. Command="{Binding Source={RelativeSource Mode=FindAncestor, AncestorType={x:Type viewmodel:PatientEdViewModel}}, Path=PatientEdViewModel.PdfViewCommand}"
  9. CommandParameter="{Binding PatientEdPdfUri}" />
  10. </ImageButton.Behaviors>
  11. </ImageButton>
  12. <Label Text="{Binding PatientEdName}" HorizontalTextAlignment="Center" FontSize="Caption" HeightRequest="30" WidthRequest="100" HorizontalOptions="Center" />
  13. </VerticalStackLayout>
  14. </DataTemplate>
  15. </CollectionView.ItemTemplate>

字符串
更新:我从UnhandledException调用的e参数中发现了这个堆栈跟踪,对我来说并不意味着什么。
在Microsoft.Maui.Controls.Binding.d__27.MoveNext()在System.Threading.Tasks.Task.<>c.b__128_0(对象状态)在Microsoft.UI.Dispatching.DispatcherWorkueSynchronizationContext.<> c__DisplayClass2_0.b_0()
最新消息:现在CollectionView加载时没有异常,点击任何ImageButton都没有效果。我的命令设置与你的例子不同。我的命令是异步的,我使用[RelayCommand]装饰。

  1. [RelayCommand]
  2. async Task PdfViewAsync(Object obj)
  3. {
  4. if (IsBusy)
  5. return;
  6. try
  7. {
  8. IsBusy = true;
  9. var model = obj as PatientEd;
  10. // Do stuff
  11. }
  12. catch (Exception ex)
  13. {
  14. Debug.WriteLine($"Unable to get Patient titles: {ex.Message}");
  15. await Shell.Current.DisplayAlert("Error!", ex.Message, "OK");
  16. }
  17. finally
  18. {
  19. IsBusy = false;
  20. }
  21. }


我在其他处理程序中使用了这种安排(对于Pickers),效果很好。这种安排对于ImageButton有什么不同吗?我在方法的顶部有一个断点,当我点击其中一个按钮时,它不会被击中。

v64noz0r

v64noz0r1#

可以将CollectionView的整个项作为命令PdfViewCommand的参数传递给ViewModel。
请参考下面的代码:

  1. <ImageButton Source="{Binding PatientEdThumbUri}" Grid.Column="2" HeightRequest="60" WidthRequest="60" BackgroundColor="Yellow" >
  2. <ImageButton.Behaviors>
  3. <toolkit:EventToCommandBehavior
  4. EventName="Clicked"
  5. Command="{Binding Source={x:Reference myCollectionView}, Path=BindingContext.PdfViewCommand}"
  6. CommandParameter="{Binding .}" />
  7. </ImageButton.Behaviors>
  8. </ImageButton>

字符串

备注:

myCollectionView是您的收藏中的x:Name视图。
然后,我们可以得到如下所示的传递参数:

  1. public ICommand PdfViewCommand => new Command<PatientEd>(DoSomething);
  2. private void DoSomething(PatientEd obj)
  3. {
  4. System.Diagnostics.Debug.WriteLine("---------> "+obj.PatientEdPdfUri);
  5. }

展开查看全部

相关问题