Home > front end >  Xamarin.Forms: TriggerAction fires only first time
Xamarin.Forms: TriggerAction fires only first time

Time:01-28

I need to set focus on a SearchBar inside view-model. I set a TriggerAction to fire a trigger when SearchBarFocused property changes his value, like so:

       ```<controls:SearchBarExtended x:Name="searchBar" Margin="-18,0,0,0" 
                                    WidthRequest="230" Placeholder="Cerca articolo" 
                                    PlaceholderColor="Gray" FontSize="16" Keyboard="Plain"                                         
                                    Text="{Binding SearchText, Mode=TwoWay}" SearchCommand="{Binding SearchCommand}" 
                                    SearchCommandParameter="{Binding Text, Source={x:Reference searchBar}}" >
            <controls:SearchBarExtended.Triggers>
                <DataTrigger TargetType="SearchBar" Binding="{Binding SearchBarFocused, Mode=TwoWay}" Value="True">
                    <DataTrigger.EnterActions>
                        <triggers:SearchBarFocusTriggerAction Focused="True" />
                    </DataTrigger.EnterActions>
                </DataTrigger>
        </controls:SearchBarExtended>```

Definition of SearchBarFocused property:

```private bool searchBarFocused;
public bool SearchBarFocused
{
    get => searchBarFocused;
    set => SetProperty(ref searchBarFocused, value);  //raise InotifyPropertyChanged
}```

This is the class that implement TriggerAction base class:

```public class SearchBarFocusTriggerAction : TriggerAction<SearchBar>
{
    public bool Focused { get; set; }

    protected override void Invoke(SearchBar searchBar)
    {
        if (Focused)
            searchBar.Focus();
        else
            searchBar.Unfocus();                                   
    }```

TriggerAction is fired only the first time. I don't know why.

CodePudding user response:

<controls:SearchBarExtended x:Name="searchBar" Margin="-18,0,0,0" 
                                    WidthRequest="230" Placeholder="Cerca articolo" 
                                    PlaceholderColor="Gray" FontSize="16" Keyboard="Plain"                                         
                                    Text="{Binding SearchText, Mode=TwoWay}" SearchCommand="{Binding SearchCommand}" 
                                    SearchCommandParameter="{Binding Text, Source={x:Reference searchBar}}" >

            <controls:SearchBarExtended.Triggers>
 <DataTrigger TargetType="SearchBar"
                     Binding="{Binding SearchBarFocused, Mode=TwoWay}"
                     Value="True">

            <Trigger.EnterActions>
                <local:FocusTriggerAction Focused="True" />
            </Trigger.EnterActions>

            <Trigger.ExitActions>
                <local:FocusTriggerAction Focused="False" />
            </Trigger.ExitActions>

        </DataTrigger>   

           
</controls:SearchBarExtended.Behaviors>

        </controls:SearchBarExtended>

then add these functions

public class FocusTriggerAction : TriggerAction<SearchBar>
{
    public bool Focused { get; set; }

    protected override async void Invoke (SearchBar searchBar)
    {
        await Task.Delay(1000);

        if (Focused)
        {
            searchBar.Focus();
        }
        else
        {
            searchBar.UnFocus();
        }
    }
}


  •  Tags:  
  • Related