同步UI => Application.Selection.Start已损坏

时间:2018-01-23 09:20:12

标签: c# vsto

使用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

1 个答案:

答案 0 :(得分:0)

问题解决了。由于某种原因,单词加载项的工作方式不同,当在同一个线程中询问游标位置时,它返回错误的值。但是一旦你启动计算方法就可以正常工作。

var selectionRange = Application.Selection.Range;
selectionRange.Calculate();
var currentSelectionStart = selectionRange.Start;