Проблема с Asyn c Task и RoutedEventHandler в WPF / C # - PullRequest
0 голосов
/ 28 января 2020

У меня есть пользовательский элемент управления с кнопкой, который используется из окна с окном RoutedEventHandler:

Контроль пользователя:

public event RoutedEventHandler IniciarPLC_Click;
private void BtnIniciarPLC_Click(object sender, RoutedEventArgs e)
{
   .....
   if (IniciarPLC_Click != null)
   {
        IniciarPLC_Click(this, new RoutedEventArgs());
   }
   ......
}

Окно :

XAML:

<ucUserControl x:Name="cntBarraHerramientas"  IniciarPLC_Click="CntBarraHerramientas_IniciarPLC_Click"/>

C#:

private void CntBarraHerramientas_IniciarPLC_Click(object sender, RoutedEventArgs e)
{
    ....       
}

Но мне нужно позвонить на async метод в CntBarraHerramientas_IniciarPLC_Click, поэтому я изменил тип возврата void на async Task и вызвал метод с await:

private async Task CntBarraHerramientas_IniciarPLC_Click(object sender, RoutedEventArgs e)
{
    await AsyncMethod(...);
}

И у меня есть эта ошибка:

Could not create a 'IniciarPLC_Click' from the text 'CntBarraHerramientas_IniciarPLC_Click'. '

ArgumentException: You cannot link to the target method because its security transparency or signature is not compatible with that of the delegated type.

Вопрос в том, как мне вызвать async метод с RoutedEventHandler? Потому что метод async, вызываемый из события нажатия кнопки, работает.

1 Ответ

2 голосов
/ 28 января 2020

Обработчик события должен быть async void. Это одно из немногих мест, где у вас будет async void вместо async Task

...