簡體   English   中英

WPF/C# 中的異步任務和 RoutedEventHandler 問題

[英]Trouble with Async Task and RoutedEventHandler in WPF/C#

我有一個帶有 Button 的用戶控件,它從帶有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)
{
    ....       
}

但是我需要調用CntBarraHerramientas_IniciarPLC_Clickasync方法,所以我通過async Task更改了void返回類型並使用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.

問題是如何使用RoutedEventHandler調用async方法? 因為從按鈕單擊事件調用的async方法有效。

事件處理程序應該是async void 這是少數幾個可以使用async void而不是async Task

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM