English 中文(简体)
Maui syntax在C#方法中使用转化器
原标题:Maui syntax to use converter inside of a C# method

What I m trying to accomplish

缩略语 观点,我列出了物体清单。 每个展览会都有一个旁边的检查箱。

理想的情况是,当用户检查或检查时,检查箱一采用一种方法,使我能够与目标站在一起:

“entergraph

以上是无耻的法典。

The actual code

在现实中,当用户检查/核对箱子时,该代码只采用一种方法,只有Im通过这个标码

I need to convert that object ChcekedChangedEventArgs into a bool. (Note that I m using the MvvM pattern, so I m not working with code-behind.)
enter image description here

A kind soul gave me some code that will probably do the converting:

public class CheckedChangedArgsConverter : BaseConverterOneWay<CheckedChangedEventArgs, bool>
{
    public override bool DefaultConvertReturnValue { get; set; } = false;

    public override bool ConvertFrom(CheckedChangedEventArgs value, CultureInfo culture)
    {
        return value.Value;
    }
}

然而,我不知道如何使用转换器。 我在此似乎有两个选择:

  1. Run the converter from the Xaml and have it run before the command AddRemoveFromList is called so I can pass in the converted Boolean, or
  2. Run the converter code from the C# class, something like this:
    enter image description here

事实是,我不知道如何做<>要么是。 谁能帮助?

================================================================================================================================================================================================================================================================

EDIT:标的<代码>ChangedEventArgs通过成为C#方法的,是ALWAYS。

Here s my Checkbox code. I m using the EventToCommandBehavior because it s the only thing I can think of to call a command from inside the Checkbox:

<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>

而这正是骗局。

问题回答

您仅可在视频模型中添加一个 b变(例如 public bool视频仪表)并用于您的视频项目实施(维德尔模型.Video.cs),然后将其与本VideoIsecked上的财产挂钩。 当然,如果你使用Nget

Once we check or uncheck the CheckBox, the value of this variable will be updated. So, we can pass the whole Video Item.

你可以提及以下法典。 在此,我继承了<代码>。 ObservableObject for the Model not INotificationpropertyChanged.

<Video.cs

public partial class Video:ObservableObject
{
    //I add a property to identify the name of the video
    public string Title { get; set; }

    [ObservableProperty]
    public bool videoIsChecked;
}

MyViewModel.cs

在这里,我们可以通过以下代码获得整个视频项目:

public ICommand AddRemoveFromListCommand { get; set; }

AddRemoveFromListCommand = new Command<Video>(DoSomething);

private void DoSomething(Video item)
    {
        if (item != null)
        {
            if (item.VideoIsChecked)
            {
                // you also need to add some other logic codes here
                System.Diagnostics.Debug.WriteLine($"{item.Title}");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"{item.Title}");

            }
        }
    }

你可以提及我的《意见》全文:

MyViewModel.cs

public class MyViewModel
{
    public ObservableCollection<Video> VideoList { get; set; }

    //define the AddRemoveFromListCommand for the CheckBox
    public ICommand AddRemoveFromListCommand { get; set; }


    public ObservableCollection<Video> SelectedVideos { get; set; }
    // you can  get all the selected Videos based on the value of property videoIsChecked
    public ICommand GetSelectedVideosCommand { get; set; }


    public MyViewModel() {
        VideoList = new ObservableCollection<Video>();// initialize the VideoList
        VideoList.Add(new Video { Title="video1", VideoIsChecked = true});
        VideoList.Add(new Video { Title="video2", VideoIsChecked = false});
        VideoList.Add(new Video { Title="video3", VideoIsChecked = false});

        SelectedVideos = new ObservableCollection<Video>(); // initialize the SelectedVideos

        AddRemoveFromListCommand = new Command<Video>(DoSomething);

        GetSelectedVideosCommand= new Command(GetAllSelectedVideos);

    }

    private void GetAllSelectedVideos()
    {
        foreach (Video video in VideoList)
        {
            if (video.VideoIsChecked)
            {
                // add the video to the selectedlist
                SelectedVideos.Add(video);
            }
        }

    }

    private void DoSomething(Video item)
    {
        if (item != null)
        {
            if (item.VideoIsChecked)
            {
                // you also need to add some other logic codes here
                System.Diagnostics.Debug.WriteLine($"{item.Title}");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"{item.Title}");

            }
        }
    }
}

3.Usage example:

 <?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:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:viewmodels="clr-namespace:MauiVideoListApp.ViewModels"
             x:Class="MauiVideoListApp.MainPage">

    <ContentPage.BindingContext>
        <viewmodels:MyViewModel></viewmodels:MyViewModel>
    </ContentPage.BindingContext>

    <VerticalStackLayout>
        <CollectionView ItemsSource="{Binding VideoList}" x:Name="myCollectionView">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <HorizontalStackLayout>
                        <CheckBox Color="#0B4C90" IsChecked="{Binding VideoIsChecked}">
                            <CheckBox.Behaviors>
                                <toolkit:EventToCommandBehavior
                                    Command="{Binding BindingContext.AddRemoveFromListCommand, Source={x:Reference myCollectionView}}"
                                    CommandParameter="{Binding .}"
                                    EventName="CheckedChanged" />
                            </CheckBox.Behaviors>

                        </CheckBox>
                        <Label Text="{Binding Title}"
                                       FontSize="20"
                                       FontAttributes="Bold"
                                       TextColor="#0B4C90"/>
                    </HorizontalStackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

        <Button Text="Get Result" Command="{Binding GetSelectedVideosCommand}"  />
    </VerticalStackLayout>

</ContentPage>

<>说明:

I also added a Button to get all the selected video list.

    // you can  get all the selected Videos based on the value of property videoIsChecked
    public ICommand GetSelectedVideosCommand { get; set; }

    GetSelectedVideosCommand= new Command(GetAllSelectedVideos);

   private void GetAllSelectedVideos()
    {
        foreach (Video video in VideoList)
        {
            if (video.VideoIsChecked)
            {
                // add the video to the selectedlist
                SelectedVideos.Add(video);
            }
        }

    }




相关问题
ASP.NET checkbox changes focus?

I have a checkbox control, and when it s clicked, the focus of the page changes to the top--the window scrolls to the top of the page. Nowhere in the code am I specifying a change in focus, either in ...

ASP.Net MVC - Handle Multiple Checkboxes

Ok, I have a role based permission system in place and would like admin s to be able to edit the permissions for each role. To do this I need to load lots of checkboxes, however I m struggling with ...

checkbox / div toggle

$(".trigger").click(function () { $(this).next(".toggle").slideToggle("fast"); $(this).toggleClass("active"); return false }); HTML: <input type="checkbox" class="trigger"> <div class=...

sticky checkbox

Ii am trying to make a checkbox (to send via email) sticky. To make sure it doesn t make a problem when sent empty I used the following: <input type="checkbox" name="something" value="1"> <...

Tri-state Check box in HTML?

There is no way to have a tri-state check button (yes, no, null) in HTML, right? Are there any simple tricks or work-arounds without having to render the whole thing by oneself?

How to get unselected checkbox?

I have three checkboxes like ch[0], ch[1] and ch[3] (sometimes i have more, or less, it s dinamic) and in PHP i want to get the unselected items also, like this: 0=yes,1=no,3=yes and so on. Can I ...

热门标签