Home > Software design >  How to fire a command after clicking somewhere else?
How to fire a command after clicking somewhere else?

Time:04-01

So, I have a custom control, CusConA, that works basically like a textbox - you type amount of money that you need, and I have a button below, whom by getting clicked saves that amount(from CusConA) somewhere, and that is working fine.

But I want to try the same functionality basically by clicking anywhere on that page (something like OnBlur in asp.net), or to be precise, when my CusConA is not in focus anymore.

By doing what is shown with the --> in code, I achieved sort of a solution, this way when pressing anywhere, even if I never even tried to write an amount, the command is being executed.

So, to try to circle my question, I need this command to execute only after typing some amount, and clicking somewhere alse after. How can I do that?

<Frame
            Margin="55,0"
            Padding="0"
            BorderColor="Blue"
            CornerRadius="30">
            <StackLayout Orientation="Horizontal">
                <Label
                    Margin="10"
                    FontAttributes="Bold"
                    FontSize="20"
                    HorizontalTextAlignment="Center"
                    Text="RSD"
                    TextColor="Some text"
                    VerticalTextAlignment="Center" />
                <customControls:CusConA
                    Margin="0,0,15,0"
                    HorizontalOptions="FillAndExpand"
                    Keyboard="Numeric"
                    Placeholder="0,00"
                    PlaceholderColor="Gray"
                    Text="Some text"
                    TextColor="Black" >
-->                  <customControls:CusConA.Behaviors>
                        <xct:EventToCommandBehavior  EventName="Unfocused" Command="{Binding DoSomething}" ></xct:EventToCommandBehavior>
                    </customControls:CusConA.Behaviors>
                </customControls:CusConA>
            </StackLayout>
        </Frame>

CodePudding user response:

Can you change DoSomething to check whether the amount has been typed? Might involve adding a boolean property to your control:

bool CanExecute { get; set; }

Then have "amount" bound to a property whose setter sets CanExecute = true; or CanExecute = false;, depending on whether an amount has been typed. Something like:

string Amount
{
    ...
    set {
        _amount = value;
        myControl.CanExecute = value.Count > 0;
    }
}

Then change DoSomething body to

if (this.CanExecute) { ... }

Alternatively, other techniques can be used to have a change to Amount trigger a change to a property on myControl.

The essential points are:

  1. Adding CanExecute property, so control can be told when it is valid to execute that command.
  2. Using some technique to bind or trigger myControl.CanExecute change, from elsewhere.

CodePudding user response:

I think you can use EventToCommandBehavior to achieve this function.

There is an example of an EventToCommandBehavior in the Xamarin.Forms samples (see here).

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

<StackLayout>
    <Entry>
        <Entry.Behaviors>
            <Behaviors:EventToCommandBehavior 
        EventName="Unfocused" 
        Command="{Binding EntryUnfocused}" />
        </Entry.Behaviors>
    </Entry>

</StackLayout>

And define EntryUnfocused in your viewmodel.cs (e.g. MyViewModel) just as follows:

MyViewModel.cs

public class MyViewModel
{
    public ICommand EntryUnfocused { get; protected set; }

    public MyViewModel() {

        EntryUnfocused = new Command(CompletedCommandExecutedAsync);
    }

    private void CompletedCommandExecutedAsync(object param)
    {
        System.Diagnostics.Debug.WriteLine("------------> come here....");
    }
}
  • Related