在 C# 方法中使用转换器的 Maui 语法

问题描述 投票:0回答:1

我想要实现的目标

CollectionView
中,我显示对象列表。每个显示屏旁边都有一个复选框。

理想情况下,当用户选中或取消选中复选框时,我会进入一个可以对对象执行操作的方法:

以上是幻想代码。

实际代码

实际上,当用户选中/取消选中该框时,代码会进入一个方法,只有我将此对象

CheckedChangedEventArgs

传递到该方法中。

我需要将该对象

ChcekedChangedEventArgs

 转换为布尔值。 (请注意,我使用的是 
MvvM
 模式,因此我不使用代码隐藏。)

一位善良的灵魂给了我一些可能会进行转换的代码:

public class CheckedChangedArgsConverter : BaseConverterOneWay<CheckedChangedEventArgs, bool> { public override bool DefaultConvertReturnValue { get; set; } = false; public override bool ConvertFrom(CheckedChangedEventArgs value, CultureInfo culture) { return value.Value; } }
但是,我不知道如何使用转换器。看来我这里有两个选择:

    从 Xaml 运行转换器,并在调用命令
  1. AddRemoveFromList
     之前运行它,以便我可以传入转换后的布尔值,或者 
  2. 从 C# 类运行转换器代码,如下所示:

事实是,我也不知道该怎么做

。有人可以帮忙吗? ====================================================== ===================

编辑:传递给 C# 方法的对象

CheckedChangedEventArgs

始终为 null。

这是我的

Checkbox

代码。我使用

EventToCommandBehavior
因为这是我能想到的从复选框内部调用命令的唯一方法:
<CheckBox x:Name="checkBox"
          ScaleX="1.5"
          ScaleY="1.5"
          Color="#000063"
          HorizontalOptions="End">
    <CheckBox.Behaviors>
        <toolkit:EventToCommandBehavior EventName="CheckedChanged"
                                        Command="{Binding Source={x:Reference this}, 
                                                  Path=BindingContext.AddRemoveFromListCommand}"/>                                       
    </CheckBox.Behaviors>
</CheckBox>

我将 
x:TypeArguments

添加到复选框中:

<CheckBox x:Name="checkBox"
          ScaleX="1.5"
          ScaleY="1.5"
          Color="#000063"
          HorizontalOptions="End">
    <CheckBox.Behaviors>
        <toolkit:EventToCommandBehavior
                x:TypeArguments="CheckedChangedEventArgs"
                                        EventName="CheckedChanged"
                                        Command="{Binding Source={x:Reference this}, Path=BindingContext.AddRemoveFromListCommand}" />
    </CheckBox.Behaviors>
</CheckBox>

这就成功了。

mvvm checkbox uicollectionview maui
1个回答
0
投票
CommandParameter

来实现此目的。

您可以参考我的演示中的以下代码:

<?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" xmlns:viewdels="clr-namespace:MauiCheckListviewApp.ViewModes" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" x:Class="MauiCheckListviewApp.MainPage"> <ContentPage.BindingContext> <viewdels:MyViewModel></viewdels:MyViewModel> </ContentPage.BindingContext> <VerticalStackLayout> <CollectionView ItemsSource="{Binding Sections}" x:Name="myCollectionView" Grid.Row="1"> <CollectionView.ItemTemplate> <DataTemplate> <HorizontalStackLayout> <CheckBox Color="#0B4C90" IsChecked="{Binding IsChecked}"> <CheckBox.Behaviors> <toolkit:EventToCommandBehavior Command="{Binding BindingContext.UpdateThisItemCommand, Source={x:Reference myCollectionView}}" CommandParameter="{Binding .}" EventName="CheckedChanged" /> </CheckBox.Behaviors> </CheckBox> <Label Text="{Binding SectionName}" FontSize="20" FontAttributes="Bold" TextColor="#0B4C90"/> </HorizontalStackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> <Button Text="Get Sections" Command="{Binding GetResultCommand}"/> </VerticalStackLayout> </ContentPage>

MyViewModel.cs

public class MyViewModel: INotifyPropertyChanged { public ObservableCollection<Item> Sections { get; set; } = new ObservableCollection<Item>(); private Item _selectedModel; public Item SelectedModel { get { return _selectedModel; } set { SetProperty(ref _selectedModel, value); if (SelectedModel != null) { System.Diagnostics.Debug.WriteLine("----> the selected Item " + SelectedModel.SectionName); Shell.Current.GoToAsync(nameof(DetailPage)); SelectedModel = null; } } } public ICommand GetResultCommand { get; set; } public ICommand UpdateThisItemCommand { get; set; } public MyViewModel() { Sections.Add(new Item { SectionName= "section_1"}); Sections.Add(new Item { SectionName= "section_2",IsChecked= true}); Sections.Add(new Item { SectionName= "section_3" }); GetResultCommand = new Command(getResult); UpdateThisItemCommand = new Command<Item>(checkboxcommand); } private void checkboxcommand(Item obj) { if (obj != null) { if (obj.IsChecked) { // you also need to add some other logic codes here System.Diagnostics.Debug.WriteLine($"{obj.SectionName}"); } else { System.Diagnostics.Debug.WriteLine($"{obj.SectionName}"); } } } private void getResult() { foreach (var section in Sections) { if (section.IsChecked) { System.Diagnostics.Debug.WriteLine(section.SectionName); } } } bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (Object.Equals(storage, value)) return false; storage = value; OnPropertyChanged(propertyName); return true; } protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }

Item.cs

public class Item: INotifyPropertyChanged { public string SectionName { get; set; } //public double Price { get; set; } private bool _isChecked; public bool IsChecked { set { SetProperty(ref _isChecked, value); } get { return _isChecked; } } bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (Object.Equals(storage, value)) return false; storage = value; OnPropertyChanged(propertyName); return true; } protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }

注:

您还可以向 Item 模型添加一个 bool 变量(例如

public bool IsChecked

)并将其绑定到此

CheckBox
的属性:
 <CheckBox Color="#0B4C90" IsChecked="{Binding IsChecked}">

一旦我们选中或取消选中
CheckBox

,它的值就会更新。

    

© www.soinside.com 2019 - 2024. All rights reserved.