Home > Blockchain >  .NET MAUI Custom control with command and parameters
.NET MAUI Custom control with command and parameters

Time:12-27

I create my custom control with 4 buttons and one command. This command, I subscribed to events Pressed and Released using EventToCommandBehavior from toolkit.

        <Button x:Name="forward" Grid.Column="1">
            <Button.ImageSource>
                <FontImageSource
                    Color="{x:Binding TextColor}"
                    BindingContext="{x:Reference forward}"
                    FontFamily="icomoon"
                    Glyph="&#xe901;" />
            </Button.ImageSource>
            <Button.Behaviors>
                <toolkit:EventToCommandBehavior
                    Command="{Binding MoveCommand}"
                    EventName="Pressed" />
                <toolkit:EventToCommandBehavior
                    Command="{Binding MoveCommand}"
                    EventName="Released" />
            </Button.Behaviors>
        </Button>          

For CommandParameter I want us my class with 2 properties. How I can put for each button object of my class with different value?

CodePudding user response:

You could use data bindings to set the CommandParameter. I share an example:

Suppose there are two parameters: Direction and State. You could generate a MoveControlEventArgs which is like:

public class MoveEventArgs : EventArgs
{
    public string Direction { get; set; }
    public string State { get; set; }
    public MoveEventArgs()
    {
    }
}

Then in the ViewModel, generate MoveEventArgs instance

public MoveEventArgs Para1 { get; set; }

public MainPageViewModel()
{
    Para1 = new MoveEventArgs() { Direction = "forward", State = "Pressed" };
}

Then in the .xaml file:

<Button.Behaviors>
    <toolkit:EventToCommandBehavior

        Command="{Binding MoveCommand}"
        CommandParameter="{Binding Para1}"
        EventName="Pressed" />
    <toolkit:EventToCommandBehavior
        Command="{Binding MoveCommand}"
        CommandParameter="{Binding Para2}"
        EventName="Released" />
</Button.Behaviors>

And for MoveCommand, we could retrieve the parameter , like the following code:

    public Command MoveCommand
    {
        get
        {
            return new Command<MoveEventArgs>((e) =>
            {
                if(e.State== "Pressed")
                {
                    Console.WriteLine("avc");
                }
                if (e.State == "Release")
                {
                    Console.WriteLine("vvv");
                }
            });
        }
    }

=============update====

Pass a simple string, you could just add a CommandParameter :

<Button.Behaviors>
    <toolkit:EventToCommandBehavior
        Command="{Binding MoveCommand}"
        CommandParameter="press:forward"
        EventName="Pressed"                
        />
    <toolkit:EventToCommandBehavior
        Command="{Binding MoveCommand}"
        CommandParameter="release:forward"
        EventName="Released" />
</Button.Behaviors>

Then in Command

public Command MoveCommand
{
    get
    {
        return new Command<string>((e) =>
        {
            if(e== "press:forward")
            {
                Console.WriteLine("avc");
            }
            ...
        });
    }
}

Hope it works for you.

  • Related