使用System.Windows.Threading.Dispatcher
同步UI的上下文,我尝试使用Word Add In的Application.Selection.Start
属性获取光标位置,并返回false值。
1)Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
2)_dispatcher.Invoke(TextChanged);
3)
private async void TextChanged()
{
var currentSelectionStart1 = Application.Selection.Start;
await Task.Delay(10);
var currentSelectionStart2 = Application.Selection.Start;
}
currentSelectionStart1 and currentSelectionStart2 are wrong
答案 0 :(得分:0)
问题解决了。由于某种原因,单词加载项的工作方式不同,当在同一个线程中询问游标位置时,它返回错误的值。但是一旦你启动计算方法就可以正常工作。
var selectionRange = Application.Selection.Range;
selectionRange.Calculate();
var currentSelectionStart = selectionRange.Start;