本文介绍了如何在执行搜索按钮async命令时从viewmodel设置焦点于WPF控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的代码后面我有一个方法OnFocusRequested,当执行异步搜索按钮命令时,我的ViewModel使用接口 调用。 问题是当我从代码中调试每一步时,它工作正常。这段代码不能实时工作。 我相信这是由于来自视图模型的那个按钮命令的异步调用 但是不知道如何解决这个并解决这个问题。 有什么建议吗? 我尝试了什么: private void OnFocusRequested( object sender,FocusRequestedEventArgs e) { switch (e.PropertyName) { case StatementDate : Application.Current.Dispatcher.BeginInvoke( new Action(()= > { dateStatement.Focus(); Keyboard.Focus(dateStatement); }), DispatcherPriority.ContextIdle, null ); break ; } } 这是我的代码隐藏文件方法,其中我设置焦点我尝试过或不带调度程序但结果是相同的。解决方案 In my code behind i have a method OnFocusRequested which is called using Interfacefrom my ViewModel when async search button command execute.the problem is that it is working fine when i just debug through each step from code. this code is not work in real time.I believe this is due to async call of that button command from view modelbut dont know tp figure out this and fix this.any suggestion ?What I have tried:private void OnFocusRequested(object sender, FocusRequestedEventArgs e) { switch (e.PropertyName) { case "StatementDate": Application.Current.Dispatcher.BeginInvoke( new Action(() => { dateStatement.Focus(); Keyboard.Focus(dateStatement); }), DispatcherPriority.ContextIdle, null ); break; } }this is my code behind file method in which i am setting the focus i have tried with or without dispatcher but result is same. 解决方案 这篇关于如何在执行搜索按钮async命令时从viewmodel设置焦点于WPF控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-24 07:38