Xamarin.Forms:TriggerAction 仅在第一次触发
Xamarin.Forms: TriggerAction fires only first time
我需要将焦点设置在视图模型内的 SearchBar 上。我将 TriggerAction 设置为在 SearchBarFocused 属性 更改其值时触发触发器,如下所示:
```<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>```
SearchBarFocused 的定义属性:
```private bool searchBarFocused;
public bool SearchBarFocused
{
get => searchBarFocused;
set => SetProperty(ref searchBarFocused, value); //raise InotifyPropertyChanged
}```
这是实现 TriggerAction 基础的 class 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 仅在第一次触发。不知道为什么。
<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>
然后添加这些函数
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();
}
}
}
我需要将焦点设置在视图模型内的 SearchBar 上。我将 TriggerAction 设置为在 SearchBarFocused 属性 更改其值时触发触发器,如下所示:
```<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>```
SearchBarFocused 的定义属性:
```private bool searchBarFocused;
public bool SearchBarFocused
{
get => searchBarFocused;
set => SetProperty(ref searchBarFocused, value); //raise InotifyPropertyChanged
}```
这是实现 TriggerAction 基础的 class 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 仅在第一次触发。不知道为什么。
<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>
然后添加这些函数
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();
}
}
}