专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

c# – 如何在Xamarin Forms中从ViewModel设置焦点

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:56:21       共计:3595 浏览

我想在做一些异步操作后将焦点设置在SearchBox控件中,我想从我的ViewModel中做到这一点.


我怎么能这样做?


编辑


ViewModel代码:


private bool _searchBarFocused;


   public bool SearchBarFocused

   {

       get { return _searchBarFocused; }

       set

       {

           _searchBarFocused = value;

           base.OnPropertyChanged("SearchBarFocused");

       }

   }


   public async Task InitializeData()

   {

       // Other operations...


       SearchBarFocused = true;

   }


查看代码隐藏代码:


protected override void OnAppearing()

   {

       base.OnAppearing();

       (this.BindingContext as MyViewModel).InitializeData();

   }


SearchBar 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();

   }

}


其中一个选项是使用触发器(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;

   }

}


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:c#-将焦点设置为Xamarin表单中的下一个条目 | ·下一条:使用StaticResource给控件定义公共的样式和属性来写界面XAML

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有