13

在进行一些异步操作后,我想在控件中设置焦点SearchBox,并且我想我的ViewModel中进行。

我怎么可能做到这一点?

编辑

视图模型代码:

    private bool _searchBarFocused;

    public bool SearchBarFocused
    {
        get { return _searchBarFocused; }
        set
        {
            _searchBarFocused = value;
            base.OnPropertyChanged("SearchBarFocused");
        }
    }

    public async Task InitializeData()
    {
        // Other operations...

        SearchBarFocused = true;
    }

View 的代码隐藏代码:

    protected override void OnAppearing()
    {
        base.OnAppearing();
        (this.BindingContext as MyViewModel).InitializeData();
    }

搜索栏 XAML 代码:

  <SearchBar SearchCommand="{Binding SearchItemsCommand}">
    <SearchBar.Triggers>
      <DataTrigger TargetType="SearchBar"
                   Binding="{Binding SearchBarFocused, Mode=TwoWay}" Value="True">
        <Trigger.EnterActions>
          <triggers:SearchBarFocusTriggerAction Focused="True" />
        </Trigger.EnterActions>

        <Trigger.ExitActions>
          <triggers:SearchBarFocusTriggerAction Focused="False" />
        </Trigger.ExitActions>
      </DataTrigger>
    </SearchBar.Triggers>
  </SearchBar>

触发动作代码:

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

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

3 回答 3

9

一种选择是使用触发器(XAML 方式):

 <SearchBar x:Name="searchBar"
       Text=""
       Placeholder="type something">
    <SearchBar.Triggers>

        <DataTrigger TargetType="SearchBar"
                     Binding="{Binding ViewModelIsSearchBarFocused}"
                     Value="True">

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

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

        </DataTrigger>   

        <EventTrigger Event="Unfocused">
            <local:UnfocusedTriggerAction />
        </EventTrigger>    

    </SearchBar.Triggers>       
</SearchBar>
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();
        }
    }
}

public class UnfocusedTriggerAction : TriggerAction<SearchBar>
{
    protected override void Invoke (SearchBar searchBar)
    {
        YourViewModel.ViewModelIsSearchBarFocused = false;
    }
}

在此处阅读更多信息:https ://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/triggers/

于 2015-08-25T23:01:13.807 回答
4

这是我如何做的一个例子,在此之前我曾经使用 MessagingCenter

在 xaml 中,您需要为要聚焦的 obj 提供一个 x:Name 。

<!-- PICKER's DEFINITION -->
<DatePicker
    x:Name="Datepicker"
    Date="{Binding SelectedDate, Mode=TwoWay}"
    IsEnabled="true"
    IsVisible="false">
</DatePicker>

那么您必须在按钮上的命令参数中引用该控件,或者例如在这种情况下我使用工具栏项。

<!-- MENU TOOLBAR -->
<ContentPage.ToolbarItems>
    <ToolbarItem
        Command="{Binding ShowCalendarCommand}"
        Icon="Calendar"
        CommandParameter="{x:Reference Datepicker}" />
</ContentPage.ToolbarItems>

然后在你的 vm 命令中:

#region toolbar commands

public ICommand ShowCalendarCommand => new RelayCommand<Object>(ShowCalendar);

#endregion

private void ShowCalendar(Object obj)
{
    var calendar = (DatePicker)obj;
    calendar.Focus();
    //  MessagingCenter.Send(this, "Calendar");
}
于 2017-09-05T13:04:00.463 回答
-1

我通过在 viewmodel 中将 contentpage 作为参数传递来解决了我的问题

BindingContext = new YourViewModel(this);

并在视图模型中创建了一个属性

public ContentPage m_View { get; set; }

然后从构造函数将值设置为此属性

 public YourViewModel(ContentPage view)
    {
        m_View = view;
    }

然后通过使用它的名称将焦点设置在我的 DatePicker 上

  var DatePicker = m_View.FindByName("YourDatePickerName") as DatePicker;
  DatePicker.Focus();

我的 DatePicker 代码是

 <DatePicker x:Name="YourDatePickerName"
             Date="{Binding SelectedDate}" />
于 2019-10-15T07:29:04.833 回答