Question

TanmayT on Fri, 21 Feb 2020 08:37:52


I am working on a VSIX Text Adornment project. Here I have LayoutChanged event and KeyUp event (from KeyProcessor). Now when I press any key (suppose Enter), both the events are eligible to be executed. Now the problem is, LayoutChanged event is executing first and it does not let KeyUp event be executed (But at the very first time I press any key, the KeyUp event is executed and then after the LayoutChanged event executed. But later, LayoutChanged is only executed).

I want KeyUp event to be always executed first. Then the rest events

Following is code for both the events and constructor:

TextAdornment1:

public TextAdornment1(IWpfTextView tv, ITextDocumentFactoryService textDocumentFactoryService)
{
    this._textDocumentFactoryService = textDocumentFactoryService;

    if (tv == null)
    {
        throw new ArgumentNullException("view");
    }

    this.layer = tv.GetAdornmentLayer("TextAdornment1");

    this.view = tv;
}

KeyUp event:

public override void KeyUp(KeyEventArgs args)
{
    this.view.LayoutChanged += OnLayoutChangedEvent;

    if (args.Key == Key.Enter)
    {
        KeyEventProcessor.layouytChangeOccured = 0;
        KeyEventProcessor.isEnterKeyUp = true;
    }
    else
    {
        KeyEventProcessor.layouytChangeOccured = 0;
        KeyEventProcessor.isEnterKeyUp = false;
        KeyEventProcessor.layouytChangeOccured = 0;
    }
}

LayoutChanged event:

internal async void OnLayoutChangedEvent(object sender, TextViewLayoutChangedEventArgs e)
{
    OnLayoutChanged2(e);
}

Replies

Sergey Vlasov on Sat, 22 Feb 2020 03:09:28


You can try to handle PreviewKeyUp instead of KeyUp. It should execute earlier.

TanmayT on Mon, 24 Feb 2020 06:50:25


It is working same as KeyUp. Is there any other way? Thanks! by the way, for answering.

Dylan Zhu-MSFT on Mon, 24 Feb 2020 10:33:15


Hi TanmayT,

Sorry for delay in reply.

From this thread, the order of event seem to be fixed, we cannot custom the priority of events by ourselves.  

Sorry for this inconvenience.

Best Regards,

Dylan

TanmayT on Mon, 24 Feb 2020 12:30:34


Okay. Is there any way to change registration order for LayoutChanged and KeyUp events? Because these two are pre defined events in c# so how can we manage the registration order? (As per my knowledge, registration is Invocation of particular event. Is that right?)

Dylan Zhu-MSFT on Tue, 25 Feb 2020 05:50:28


Hi TanmayT,

Thank you for reply.

About your latest question, it seems to be beyond our support, and it is more related to C# language. May you could move to Visual C# forum, and then you could get dedicated support there.

Sorry for this inconvenience.

Best Regards,

Dylan